tags:

views:

348

answers:

7

I really like the strtotime() function, but the user manual doesn't give a complete description of the supported date formats. strtotime('dd/mm/YYYY') doesn't work, it works only with mm/dd/YYYY format.


if i have date in dd/mm/YYYY format, ho can i convert it to YYYY-mm-dd? i can do it by using explode() function, but i think tere are better solution.

Thanks

+4  A: 

Here is the simplified solution:

$date = '25/05/2010';
$date = str_replace('/', '-', $date);
echo date('Y-m-d', strtotime($date));

Result:

2010-05-25
Web Logic
@Web Logic i can't anderstand the default format of strtotime() function, if i write strtotimr('01-01-2010'), it understand it as dd-mm-YYYY or mm-dd-YYYY format?
Syom
If you test it with: `echo date('Y-m-l', strtotime('01-01-2010'));` The result is `2010-01-Friday`. So day coming at the end, depends on how you specify the date format to the `date` function.
Web Logic
@Web Logic thanks man
Syom
@Syom: You are welcome :)
Web Logic
+1  A: 

I haven't found a better solution. You can use explode(), preg_match_all(), etc.

I have a static helper function like this

class Date {

    public static function ausStrToTime($str) {
        $dateTokens = explode('/', $str);
        return strtotime($dateTokens[1] . '/' . $dateTokens[0] . '/' . $dateTokens[2]); 

    }

}

There is probably a better name for that, but I use ausStrToTime() because it works with Australian dates (which I often deal with, being an Australian). A better name would probably be the standardised name, but I'm not sure what that is.

alex
A: 

Are you getting this value from a database? If so, consider formatting it in the database (use date_format in mysql, for example). If not, exploding the value may be the best bet, since strtotime just doesn't seem to appreciate dd/mm/yyyy values.

Joseph Mastey
@Joseph Mastey, no, it's value from jquery calendar
Syom
A: 

If you know it's in dd/mm/YYYY, you can do:

    $regex = '#([/d]{1,2})/([/d]{1,2})/([/d]{2,4})#';
    $match = array();
    if (preg_match($regex, $date, $match)) {
        if (strlen($match[3]) == 2) {
            $match[3] = '20' . $match[3];
        }
        return mktime(0, 0, 0, $match[2], $match[1], $match[3]);
    }
    return strtotime($date);

It will match dates in the form d/m/YY or dd/mm/YYYY (or any combination of the two)...

If you want to support more separators than just /, you can change the regex to:

    $regex = '#([\d]{1,2})[/-]([\d]{1,2})[/-]([\d]{2,4})#';

And then add any characters you want into the [/-] bit (Note, the - character needs to be last)

ircmaxell
Surely your regex should be using `\d` for decimal digits rather than `[/d]` for *forward-slash or d character*.
salathe
A: 

You can parse dates from a custom format (as of PHP 5.3) with DateTime::createFromFormat

$timestamp = DateTime::createFromFormat('!d/m/Y', '23/05/2010')->getTimestamp();

(Aside: The ! is used to reset non-specified values to the Unix timestamp, ie. the time will be midnight.)


If you do not want to (or cannot) use PHP 5.3, then a full list of available date/time formats which strtotime accepts is listed on the Date Formats manual page. That page more thoroughly describes the fact that m/d/Y is inferred over d/m/Y (but you can, as mentioned in the answers here, use d-m-Y, d.m.Y or d\tm\tY).


In the past, I've also resorted to the quicky str_replace mentioned in another answer, as well as self-parsing the date string into another format like

$subject   = '23/05/2010';
$formatted = vsprintf('%3$04d/%2$02d/%1$02d', sscanf($subject, '%02d/%02d/%04d'));
$timestamp = strtotime($formatted);
salathe
A: 

You don't need "better" solution for such a trifle thing. Just do it as you can.

Col. Shrapnel
A: 

The simplest solution is this:

$date    = '07/28/2010';
$newdate = date('Y-m-d', strtotime($date));
simply_jhen