Jump to content

Strange behavior with strtotime()


lococobra

Recommended Posts

strtotime() is not magic, it only understand specific input. Your "first day" is simply (quite rightly) being interpreted as a movement relative to the specified timestamp, basically "add one day". 

 

To get the first of the month, you must use "first day of" which was added to the date string parsing in PHP 5.3.0.  Without that being available to you, an alternative is to use mktime() or as jdavidbakr pointed out, just fake it.

But hold on a second. If I execute (with PHP 5.3.2 on Windows)

php -r "echo date('r', strtotime('first day', 1291064453));"

I get

Mon, 01 Nov 2010 13:00:53 -0700

 

Must be a difference between the Windows and Linux versions of php, I get:

 

$ php -r "echo date('r', strtotime('first day', 1291064453));"
Tue, 30 Nov 2010 15:00:53 -0600

 

php 5.2.13 on Linux

 

I don't know what I would expect 'first day' to return, and don't see an official list of commands in the strtotime() manual page.

Must be a difference between the Windows and Linux versions of php, I get:

Could definitely be. strtotime is a Linux thing so to work on Windows the PHP devs probably had to rewrite/port it. $discrepancies++;

 

I don't know what I would expect 'first day' to return, and don't see an official list of commands in the strtotime() manual page.

Check the GNU page.

But hold on a second. If I execute (with PHP 5.3.2 on Windows)

php -r "echo date('r', strtotime('first day', 1291064453));"

I get

Mon, 01 Nov 2010 13:00:53 -0700

 

There are differences between PHP versions, "first day" will do as you describe for versions 5.3.0 through 5.3.2 inclusive, but not earlier or later versions (or future).

 

The formats that strtotime and the DateTime class accept are documented here: http://php.net/datetime.formats. What formats are available when could be clarified but you get the idea.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.