Jump to content

strtotime() won't work with american-style dates...


stapler

Recommended Posts

searched for a while but can't find out where to configure this...I want to convert a date string like "2-15-08" to a standardized "mm/dd/yyyy" format, i.e. "02/15/2008", so I thought I could do date("m/d/Y", strtotime("2-15-08")) but for some reason strtotime() is only working for me if it's a european date style, i.e. "15-2-08"...my time zone is properly set to central time, and, if it's at all relevant, date_ default_ timezone_ get() returns "American/Chicago" so it knows I'm in the US....what n00b thing am I missing here? thanks.

Link to comment
Share on other sites

strtotime is very fussy about its stuff

 

if your date is consitently in  m/dd/yy

you can try treating it as a string, exploding it at the "/" and then reforming into a tiemstamp with the mktime() function.

 

 

strtotime should really only be used for its "+1 day", "-1 year" ability to adjust times and minimal other cases.

Link to comment
Share on other sites

there is a lotta stuff it doesn't like which is why I basically treated it as the blacksheep to the php date family.  mktime is so much easier + you can always check consitentcy of values like saying

<?php
$date = "12/2/2008";
list($month,$day,$year) = explode("/",$date);
if(intval($month) < 1 || intval($month) >12){
#invalid month
}
if(intval($day) <28 || intval($day) >31){
#invalid day
}

Link to comment
Share on other sites

When strtotime() sees a string with dashes between the numbers it assumes it's in the format of yyyy-mm-dd or yy-mm-dd

 

2-15-2008 doesn't work, when it test with

<?php
        echo date('m/d/Y',strtotime($argv[1])) . "\n";
?>

Using the CLI version, I get

$ php -q -f testdt.php 2008-02-15
02/15/2008
$ php -q -f testdt.php 15-02-08
02/08/2015
$ php -q -f testdt.php 2-15-2008
12/31/1969
$ php -q -f testdt.php 08-08-08
08/08/2008
$ php -q -f testdt.php 08-07-06
07/06/2008
$ php -q -f testdt.php 2-15-2008
12/31/1969

 

Ken

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.