Time and Date formatting help

Hey there kirupians

I have a string of a date in a (‘YmdHis’) format ie: 20050102101215 is the 2nd of january, 2005 at 10:12. I like this format because it’s relatively easy to read, you can sort objects chronologically with it and it doesn’t contain words (‘wed,tue’,‘sep,oct’).

HOwever I have a problem converting it back to a proper date object. I’d like to use the mktime function to calculate date differences and all, but there’s nothing to help me turn 20050102101215 back into the proper date.

I was thinking along the lines of:


$oldate = '20050102101215'
$newdate = strftdate($olddate,'YmdHis','r,);
echo $olddate;
// prints Jan 2nd 2005, 10:12 am(GMT+1)

But can’t find the appropriate function to do so. Help :angel:

cheers,
mlk

You want a function that will take “20050102101215” and display it as “Jan 2nd 2005, 10:12 am(GMT+1)”?

There’s no (native) PHP function that I can think of that will do that. You’ll have to make your own.

<?php

function showDate($dateString){
  $year   = substr($dateString, 0, 4);
  $month  = substr($dateString, 4, 2);
  $day    = substr($dateString, 6, 2);
  $hour   = substr($dateString, 8, 2);
  $minute = substr($dateString, 10, 2);
  $second = substr($dateString, 12, 2);

  $unixTime = mktime($hour, $minute, $second, $month, $day, $year);
  return date("M jS Y, g:i a (\G\M\TO)", $unixTime);
}

$ds = "20050102101215";
$theDate = showDate($ds);

echo "Date string: $ds <br /> New Date: $theDate";

?>

You can break your original date string however you see fit. I just used substr() since it was a simple example. And you don’t really need to save each of those substring in variables like I did either, you could just use the substr() function right in the mktime() function… Or make it one long line if you like to punish yourself. :slight_smile:

function showDate($dateString){
  return date("M jS Y, g:i a (\G\M\TO)", mktime(substr($dateString, 8, 2), substr($dateString, 10, 2), substr($dateString, 12, 2), substr($dateString, 4, 2), substr($dateString, 6, 2), substr($dateString, 0, 4)));
}

Check out this function: strptime. It’s only available since (PHP 5 >= 5.1.0RC1) though, so it may not be available on your system. It parses a date generated by strftime (which is what yours could be interpreted as). Basically, you provide the format of the timestamp using strftime’s format (%Y%m%d%H%M%S in your case), and it returns an array containing info about the timestamp like this:

Array
(
    [tm_sec] => 19
    [tm_min] => 54
    [tm_hour] => 15
    [tm_mday] => 3
    [tm_mon] => 9
    [tm_year] => 104
    [tm_wday] => 0
    [tm_yday] => 276
    [unparsed] =>
}

Those you can use again with mktime to re-format as you wish.

Thanks to both of you, will try strptime. My host is usually up to date on php so it should be available.

:slight_smile: Lucky… my host typically isn’t that up to date! Which is why I posted a homemade function. Well that and I don’t like needing to convert most of the information from strptime() - I mean normally I don’t work with a 24 hour clock for getting back the number of hours after midnight means I need to convert to AM/PM on my own anyway.