PHP Dates and Times Parsing Dates and Times from Strings - Web Development and Design | Tutorial for Java, PHP, HTML, Javascript PHP Dates and Times Parsing Dates and Times from Strings - Web Development and Design | Tutorial for Java, PHP, HTML, Javascript

Breaking

Post Top Ad

Post Top Ad

Sunday, May 12, 2019

PHP Dates and Times Parsing Dates and Times from Strings

PHP Dates and Times





Parsing Dates and Times from Strings


Problem

You need to get a date or time in a string into a format you can use in calculations. For example, you want to convert date expressions such as “last Thursday” or “February 9, 2004” into an epoch timestamp.

Solution

The simplest way to parse a date or time string of arbitrary format is with strto time(), which turns a variety of human-readable date and time strings into epoch timestamps, as shown in example.


Example   Parsing strings with strtotime()

            $a = strtotime('march 10'); // defaults to the current year
            $b = strtotime('last thursday');
            $c = strtotime('now + 3 months');

Discussion

The grammar strtotime() uses is both complicated and comprehensive. It incorporates the GNU Date Input Formats specification (which is available from GNU) and some extensions. The function strtotime() understands words about the current time:

            $a = strtotime('now');
            print date(DATE_RFC850, $a);
            print "\n";

            $a = strtotime('today');
            print date(DATE_RFC850, $a);

            Tuesday, 12-Feb-13 19:12:14 UTC
            Tuesday, 12-Feb-13 00:00:00 UTC


It understands different ways to identify a time and date:

            $a = strtotime('5/12/2014');
            print date(DATE_RFC850, $a);
            print "\n";
            $a = strtotime('12 may 2014');
            print date(DATE_RFC850, $a);

            Monday, 12-May-14 00:00:00 UTC
            Monday, 12-May-14 00:00:00 UTC


It understands relative times and dates:

            $a = strtotime('last thursday'); // On February 12, 2013
            print date(DATE_RFC850, $a);
            print "\n";
            $a = strtotime('2015-07-12 2pm + 1 month');
            print date(DATE_RFC850, $a);

            Thursday, 07-Feb-13 00:00:00 UTC
            Wednesday, 12-Aug-15 14:00:00 UTC


It understands time zones. In the following code, the time part (2pm) doesn’t change because both PHP’s default time zone identifier (America/New_York) and the time zone in the string passed to strtotime() are the same (EDT is the time zone abbreviation for daylight saving time in New York):

            date_default_timezone_set('America/New_York');
            $a = strtotime('2012-07-12 2pm America/New_York + 1 month');
            print date(DATE_RFC850, $a);

            Sunday, 12-Aug-12 14:00:00 EDT


However, with PHP’s default time zone identifier set to America/Denver (two hours before America/New_York), the same string passed to strtotime() produces the time in New York when it is 2 P.M. in Denver (two hours before New York):

            date_default_timezone_set('America/New_York');
            $a = strtotime('2012-07-12 2pm America/Denver + 1 month');
            print date(DATE_RFC850, $a);

            Sunday, 12-Aug-12 16:00:00 EDT


The same extensive grammar that strtotime() uses is also applied when creating a DateTime object. So, although strtotime() is very useful if you just need an epoch timestamp, you can pass the same strings to new DateTime() to build a DateTime object for further manipulation.

If you find yourself with a date or time string with a known format, but that is not parseable by strtotime(), you can still create DateTime objects based on the string by using DateTime::createFromFormat(). Example shows how to use Date Time::createFromFormat() to parse date strings written in day-month-year order. (PHP’s default is month-day-year order.)


Example   Parsing a date with a specific format

            $dates = array('01/02/2015', '03/06/2015', '09/08/2015');

            foreach ($dates as $date) {
                      $default = new DateTime($date);
                      $day_first = DateTime::createFromFormat('d/m/Y|', $date);
                      printf("The default interpretation is %s\n but day-first is %s.\n",
                                $default->format(DateTime::RFC850),
                                $day_first->format(DateTime::RFC850));
            }

Example   prints:

            The default interpretation is Friday, 02-Jan-15 00:00:00 UTC
            but day-first is Sunday, 01-Feb-15 00:00:00 UTC.
            The default interpretation is Friday, 06-Mar-15 00:00:00 UTC
            but day-first is Wednesday, 03-Jun-15 00:00:00 UTC.
            The default interpretation is Tuesday, 08-Sep-15 00:00:00 UTC
            but day-first is Sunday, 09-Aug-15 00:00:00 UTC.



No comments:

Post a Comment

Post Top Ad