How I Learned To Stop Using strtotime() And Love PHP DateTime

1st March 2013

The DateTime classes in PHP have been available since version 5.2, but I have largely ignored them until recently. This was partly due to the fact that I was working in PHP 5.1 environments a lot (don't ask) but mostly because I was just used to using the standard date functions that have always been a part of PHP (well, since version 4). I wanted to explain why I will be using the new DateTime classes more from now on and why you shouldn't be hesitant to use them.

Using a combination of strtotime() and date() can handle most things and is a good method to quickly grab a date.

  1. echo strtotime("02/23/2013 12:00"); // prints 1361620800
  2. echo strtotime("today"); // prints 1362096000
  3. echo strtotime("2013-02-23"); // prints 1362096000

You can get real problems, however, when the string isn't quite what strtotime() expects, which is especially true when trying to parse a date in a non-US date format. If the date string isn't understood by strtotime() then it will return false.

var_dump(strtotime("23/02/2013")); // prints bool(false)

When this happens you can try a few things to force strtotime() to parse the date correctly. Sometimes it's something as simple as swapping the slashes for dashes, which forces strtotime() to parse the date in a different way.

  1. $date = "23/02/2013";
  2. $timestamp = strtotime($date);
  3. if ($timestamp === FALSE) {
  4. $timestamp = strtotime(str_replace('/', '-', $date));
  5. }
  6. echo $timestamp; // prints 1361577600

However, this gets more complicated when the date string consists of more than just the date. Take the following date string, which I found being returned from a web service I was using.

Thu, 23/02/2012 - 15:18

Trying the previous trick of attempting to replace slashes doesn't work here, so further steps needed to be taken. After a couple of minutes of playing around with the string I found that removing the dash (-) between the date and the time caused the strtotime() function to accept the string and parse it correctly.

$timestamp = strtotime(trim(str_replace('/', '-', str_replace('-', '', $date))));

It was at this point that I questioned what I was trying to do. In my attempt to get the date string parsed I had used three expensive string manipulation functions, just to try and get the date time string into a state that strtotime() could understand. I therefore resolved to find a better solution to this problem and after a bit of research I re-discovered the PHP DateTime classes. Of particular interest was a method called DateTime::createFromFormat() that takes a format and a string and creates a date object. Here is an example of using the createFromFormat() method to parse the above date time value correctly.

  1. $str_date = 'Thu, 23/02/2012 - 15:18';
  2. $obj_date = DateTime::createFromFormat('D, d/m/Y - H:i', $str_date);
  3. echo $obj_date->getTimestamp(); // prints 1330010280

My ultimate goal in all this was to simply compare one date with another as part of a usort() function call. What I initially started doing was converting the time into a timestamp and then compared those timestamps. With the PHP DateTime classes this isn't needed as the objects are directly comparable (at least since version 5.2.2). Here is an example of comparing two different DateTime objects.

  1. $str_date = 'Thu, 23/02/2012 - 15:18';
  2. $obj_date_1 = DateTime::createFromFormat('D, d/m/Y - H:i', $str_date);
  4. $str_date = 'Thu, 08/11/2012 - 12:57';
  5. $obj_date_2 = DateTime::createFromFormat('D, d/m/Y - H:i', $str_date);
  7. var_dump($obj_date_1 < $obj_date_2); // prints bool(true)

Rather than attempt to hack the string into a working format and push it into strtotime() it is best to use the DateTime classes so that you are absolutely sure of what dates you get from your inputs. In fact, if you are unsure of the input format of your date then you need to take a step back and understand why this is the case. You have to be sure about what input parameters are being passed to any program, and if you are receiving a mix of different date formats then things can go horribly wrong very quickly.

The good thing about the createFromFormat() method is that if you do give it the wrong date format then it will return false. This means that you do have some leeway when creating DateTime objects and can correctly detect if things are not as they appear to be.

  1. $str_date = 'Thu, 23/02/2012';
  2. $obj_date = DateTime::createFromFormat('D, d/m/Y - H:i', $str_date);
  3. var_dump($obj_date); //prints bool(false)

This is much more robust than using the DateTime class constructor which will throw an exception if you supply it with an invalid date format. Here is an example of producing an exception with the PHP DateTime class.

  1. try {
  2. $date = new DateTime('11n5fgfgh');
  3. } catch (Exception $e) {
  4. echo $e->getMessage();
  5. exit(1);
  6. }

This produces the following output.

DateTime::__construct(): Failed to parse time string (11n5fgfgh) at position 0 (1): Unexpected character

The bottom line in all of this is that if you are accepting user input (or input of any kind) then you need to do all you can to make sure it is in the correct format before you start working on it. If you are using a web service and the date format keeps changing then you need to tell them about this as that is really poor practice.

A couple of years ago I had first hand experience of maintaining a system (I didn't build it, I hasten to add!) where there was a single function to manipulate dates, but I couldn't be sure in what format the date would be passed in due to different parts of the system recording dates differently. The system would call this single function and either pass a null value or one of four different date formats. This meant a few occasions where strtotime() would fail and return false, which was then returned as Jan 1st 1970 by the date() function, which was then saved back to the database. I eventually had to track down every place where this function was called from and make sure it was passing the correct value. That pain would have been far easier if I had swapped out the function with a DateTime() object as then I would know for sure if the date was read correctly.



Angelos (Fri, 05/09/2014 - 20:13)

Thank you

Gary Benade (Fri, 05/23/2014 - 16:00)

Thanks a looooootttt excelent explanation!!

Walter (Mon, 07/28/2014 - 22:36)

Thanks, it really helped.

flashfs (Mon, 10/06/2014 - 15:28)


Anonymous (Thu, 04/07/2016 - 05:25)

In your example: $str_date = 'Thu, 23/02/2012 - 15:18'; I would like to get the date to be a string so that I can insert it be something along the lines of yesterday's date at 11:59 PM. It would need to be dynamic though. Any ideas?

Carl (Fri, 03/24/2017 - 14:42)

I think you want something like this: $dateinterval = DateInterval::createFromDateString('-1 day');

philipnorton42 (Fri, 03/24/2017 - 14:58)

thanks In your example: $str_date = 'Thu, 23/02/2012 - 15:18'; I would like to get the date to be a string , I want to get month from it I try $variable_added=date("m", strtotime ($list_item -> field_birthday ) ); but it failed , Any Ideas?

kenana (Thu, 05/11/2017 - 10:20)

This is great. Appreciate the info. Learned something new about DateTime which I like a lot!

Andy (Thu, 06/15/2017 - 17:23)

Thanks for the info, really helped.

Chris (Wed, 03/14/2018 - 08:59)

Add new comment

The content of this field is kept private and will not be shown publicly.