Parsing a Date or Time in PHP

To create a DateTime object that represents a specific time, pass that time as a first argument to the constructor. This argument is a string indicating the date and time you want the object to represent. A DateTime object understands a very wide variety of format strings. Whatever you are dealing with probably works, but an exhaustive list of all the possible formats is available at http://www.php.net/datetime.formats.

Example 15-3 shows a few date-and-time formats that the DateTime constructor understands.

Example 15-3. Formatted date/time strings that DateTime understands

// If only a time is supplied, the current date is used for day/month/year

$a = new DateTime(’10:36 am’);

// If only a date is supplied, the current time is used for hour/minute/second

$b = new DateTime(‘5/11’);

$c = new DateTime(‘March 5th 2017’);

$d = new DateTime(‘3/10/2018’);

$e = new DateTime(‘2015-03-10 17:34:45’);

// DateTime understands microseconds

$f = new DateTime(‘2015-03-10 17:34:45.326425’);

// Epoch timestamp must be prefixed with @

$g = new DateTime(‘@381718923’);

// Common log format

$h = new DateTime(‘3/Mar/2015:17:34:45 +0400’);

// Relative formats, too!

$i = new DateTime(‘next Tuesday’);

$j = new DateTime(“tast day of April 2015”);

$k = new DateTime(“November 1, 2012 + 2 weeks”);

At noon on October 20, 2016, the full dates and times that would end up in the vari­ables in Example 15-3 would be:

Thu, 20 Oct 2016 10:36:00 +0000

Wed, 11 May 2016 00:00:00 +0000

Sun, 05 Mar 2017 00:00:00 +0000

Sat, 10 Mar 2018 00:00:00 +0000

Tue, 10 Mar 2015 17:34:45 +0000

Tue, 10 Mar 2015 17:34:45 +0000

Fri, 05 Feb 1982 01:02:03 +0000

Tue, 03 Mar 2015 17:34:45 +0400

Tue, 25 Oct 2016 00:00:00 +0000

Thu, 30 Apr 2015 00:00:00 +0000

Thu, 15 Nov 2012 00:00:00 +0000

If you have discrete date-and-time parts, such as those submitted from form elements in which a user can specify month, day, and year or hour, minute, and second, you can also pass them to the the setTime() and setDate() methods to adjust the time and date stored inside the DateTime object.

Example 15-4 shows setTime() and setDate() at work.

Example 15-4. Setting date or time parts

// $_POST[‘mo’], $_POST[‘dy’], and $_POST[‘yr’]

// contain month number, day, and year submitted

// from a form

//

// $_POST[‘hr’], $_POST[‘mn’] contain

// hour and minute submitted from a form

 

// $d contains the current time, but soon that will

// be overridden

$d = new DateTime();

$d->setDate($_POST[‘yr’], $_POST[‘mo’], $_POST[‘dy’]);

$d->setTime($_POST[‘hr’], $_POST[‘mn’]);

print $d->format(‘r’);

If $_POST[‘yr’] is 2016, $_POST[‘mo’] is 5, $_POST[‘dy’] is 12, $_POST[‘hr’] is 4, and $_POST[‘mn’] is 15, then Example 15-4 prints:

Thu, 12 May 2016 04:15:00 +0000

Even though $d is initialized to the current date and time when Example 15-4 is run, the calls to setDate() and setTime() change what’s stored inside the object.

The DateTime object tries to be as accommodating as possible when parsing incom­ing data. Sometimes this is helpful, but sometimes it is not. For example, consider what you think should happen if, in Example 15-4, $_POST[‘mo’] is 3 and $_POST[‘dy’] is 35. It can never be the 35th of March. That doesn’t bother DateTime, though. It considers March 35 to be the same as April 4 (March 31 is the last day of March, so March 32 is the next day (April 1), March 33 is April 2, March 34 is April 3, and March 35 is April 4). Calling $d->setDate(2016, 3, 35) gives you a DateTime object set to April 4, 2016.

For stricter validation of days and months, use checkdate() on the month, day, and year first. It tells you whether the provided month and day are valid for the provided year, as shown in Example 15-5.

Example 15-5. Verifying months and days

if (checkdate(3, 35, 2016)) {

print “March 35, 2016 is OK”;

}

if (checkdate(2, 29, 2016)) {

print “February 29, 2016 is OK”;

}

if (checkdate(2, 29, 2017)) {

print “February 29, 2017 is OK”;

}

In Example 15-5, only the second call to checkdate() returns true. The first fails because March always has fewer than 35 days, and the third fails because 2017 is not a leap year.

Source: Sklar David (2016), Learning PHP: A Gentle Introduction to the Web’s Most Popular Language, O’Reilly Media; 1st edition.

Leave a Reply

Your email address will not be published. Required fields are marked *