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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.