views:

503

answers:

4

I have dates in the following format (yyyymmdd, 18751104, 19140722)... what's the easiest way to convert it to date().... or is using mktime() and substrings my best option...?

+4  A: 

Use strtotime to convert a string containing a date to a Unix timestamp:

<?php
// both lines output 813470400
echo strtotime("19951012"), "\n",
     strtotime("12 October 1995");
?>

You can pass the result as the second parameter to date() to reformat the date yourself:

<?php
// prints 1995 Oct 12
echo date("Y M d", strtotime("19951012"));
?>

Note

strtotime() will fail with dates before the Unix epoch at the start of 1970.

As an alternative which will work with dates before 1970:

<?php
// Returns the year as an offset since 1900, negative for years before
$parts = strptime("18951012", "%Y%m%d");
$year = $parts['tm_year'] + 1900; // 1895
$day = $parts['tm_mday']; // 12
$month = $parts['tm_mon']; // 10
?>
meagar
It won't work on dates prior to 1970....echo date("F j, Y", strtotime(19951012));echo date("F j, Y", strtotime(18751104));echo date("F j, Y", strtotime(19140722));Warning: date(): Windows does not support dates prior to midnight (00:00:00), January 1, 1970 in c:\users\svernei\desktop\irails\www\sand.php on line 28Linux just spits out 1969... for the eighteenth hundred...October 12, 1995December 31, 1969July 22, 1914
Serhiy
the 1875 one breaks in linux for me as well... 18751104 returns 1969 July 22... though it does handle 1914 while windows can't
Serhiy
@Serhiy strtotime() returns false for dates before 1970, so in essence you're calling date(0), which will return Dec 31st 23:59:59 1969
meagar
+2  A: 

Personally, I'd just use substr() because it's probably the lightest way to do it anyway.

But here's a function that takes a date, of which you can specify the format. It returns an associative array, so you could do for example (untested):

$parsed_date = date_parse_from_format('Ymd', $date);
$timestamp = mktime($parsed_date['year'], $parsed_date['month'], $parsed_date['day']);

http://uk.php.net/manual/en/function.date-parse-from-format.php

Although I must say, I don't find that any easier or more effective than simply:

mktime(substr($date, 0, 4), substr($date, 4, 2), substr($date, 6, 2));
Helgi Hrafn Gunnarsson
meagar's solution is better. It actually works, too! ;)
Helgi Hrafn Gunnarsson
I had voted for this as the answer... but mktime suffers from the same 1970 problem as the other answer... and that arrangement for mktime is a bit off...
Serhiy
A: 

have a look at strptime

knittl
A: 

Well thanks for all the answers but the 1900 problem seems to plague every response I got. Here is a copy of the function I am using should someone find it useful for them in the future.

public static function nice_date($d){
    $ms = array(
           'January',
           'February',
           'March',
           'April',
           'May',
           'June',
           'July',
           'August',
           'September',
           'October',
           'November',
           'December'
    );

    $the_return = '';
    $the_month = abs(substr($d,4,2));
    if ($the_month != 0) {
        $the_return .= $ms[$the_month-1];
    }

    $the_day = abs(substr($d,6,2));
    if ($the_day != 0){
        $the_return .= ' '.$the_day;
    }

    $the_year = substr($d,0,4);
    if ($the_year != 0){
        if ($the_return != '') {
            $the_return .= ', ';
        }
        $the_return .= $the_year;
    }

    return $the_return;
}
Serhiy