PHP | strptime() Function

PHP strptime() Function


Hello dear readers! welcome back to another edition of our tutorial on PHP. In this tutorial guide, we are going to be discussing about the PHP strptime() Function.

The strptime() function accepts a date string (generated with the strftime()) and formats a string as parameters and, parses the given string in the specified format.

Syntax

Following below is the syntax to use this function -

strptime($date, $format)


Parameter Details

Sr.NoParameter & Description
1

date(Mandatory)

This is a string value representing the date to parse.

2

format(Mandatory)

This is a string value representing the format used to parse the date.


Return Value

This PHP function returns an array which contains the parsed data. In case of failure, this PHP function returns the boolean value false. The array returned contains the following keys &minus -

  • [tm-sec] - seconds (0-61)
  • [tm-min] - minutes (0-59)
  • [tm-hour] - hour (0-23)
  • [tm-mday] - This is the day of the month (1-31)
  • [tm-mon] - This is the month since January (0-11)
  • [tm-year] - year since 1900
  • [tm-wday] - This is the days since Sunday (0-6)
  • [tm-yday] - This is the days since January 1 (0-365)
  • [unparsed] - the part of the date that was unrecognized using the specified format, if any.

PHP Version

This function was first introduced as part of core PHP version 5.1.0 and, it works with all of the later versions.

Example1

Following example demonstrates the usage of the PHP strptime() function -

<?php
   $format = '%A %d %B %G %T';
   $strf = strftime($format);
   $res = strptime($strf, $format);
   print_r($res);
?>

Output

When the above code is executed, it will produce the following result -

Array ( [tm_sec] => 10 [tm_min] => 3 [tm_hour] => 1 [tm_mday] => 23 [tm_mon] => 10 [tm_year] => 0 [tm_wday] => 1 [tm_yday] => 326 [unparsed] => )

Example2

Try the following example -

<?php
   $format = '%d/%m/%Y %H:%M:%S';
   $strf = strftime($format);
   
   echo "$strf\n";
   
   print_r(strptime($strf, $format));
?>

Output

When the above code is executed, it will produce the following result -

23/11/2020 01:05:33 Array ( [tm_sec] => 33 [tm_min] => 5 [tm_hour] => 1 [tm_mday] => 23 [tm_mon] => 10 [tm_year] => 120 [tm_wday] => 1 [tm_yday] => 327 [unparsed] => )


Following below are the various characters to format the date and time using strftime &minus -

  • %a - Abbreviated weekday name.
  • %A - Full weekday name.
  • %b - An abbreviated month name.
  • %B - Full month name.
  • %c - Preferred date and time representation.
  • %C - Century number (year divided by 100, ranges from 00 to 99).
  • %d - Day of the month (01 to 31).
  • %D - Same as %m/%d/%y.
  • %e - Day of the month (1 to 31).
  • %g - Like %G, but without the century.
  • %G - Its is a 4-digit year that correspond to the ISO week number (see %V).
  • %h - Same as %b.
  • %H - Hours, using a 24-hour clock (00 to 23).
  • %I - Hours, using a 12-hour clock (01 to 12).
  • %j - Day of the year (001 to 366).
  • %m - Month (01 to 12).
  • %M - Minute.
  • %n - Newline character.
  • %p - Either am or pm based on the given time value.
  • %r - Time in a.m. and p.m. notation.
  • %R - This is time in 24-hour notation.
  • %S - Second.
  • %t - Tab character.
  • %T - Current time, equal to %H/%M/%S.
  • %u - Weekday as a number (1 to 7), Monday=1. Note: In Sun Solaris, Sunday=1.
  • %U - It's the week number of the current year, beginning with the first Sunday as the first day of the first week.
  • %V - It is the ISO 8601 week number of the current year (01 to 53), where week 1 is the first week with at least 4 days in the current year, and with Monday as the first day of the week.
  • %W - Its the week number of the current year, beginning with first Monday as the first day of the first week.
  • %w - It's the day of the week as a decimal, Sunday=0.
  • %x - It is the preferred date representation without time.
  • %X - It is the preferred time representation without date.
  • %y - This is the year without a century (00 to 99).
  • %Y - It is the year including the century.
  • %Z or %z - The time zone or name or abbreviation.
  • %% - A literal % character.


Alright guys! This is where we are rounding up for this tutorial post. In our next tutorial guide, we are going to be discussing about the PHP strtotime() Function.

Do feel free to ask your questions where necessary and i will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.

Thanks for reading and bye for now.