# Time and Date formatting help

**URL:** <https://forum.kirupa.com/t/time-and-date-formatting-help/184895>\
**Category:** programming\
**Created:** [April 8, 2006, 10:07pm UTC](https://forum.kirupa.com/t/time-and-date-formatting-help/184895 "2006-04-08T22:07:28Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mlk](https://avatars.discourse-cdn.com/v4/letter/m/e56c9b/32.png) [@mlk](https://forum.kirupa.com/u/mlk)\
**Post date:** [April 8, 2006, 10:07pm UTC](https://forum.kirupa.com/t/time-and-date-formatting-help/184895/1 "2006-04-08T22:07:28Z")

</div>

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:

```auto

$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 👼

cheers,  
mlk

---

<div class="post-metadata">

**Author:** ![Ankou](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/ankou/32/1351_2.png) [@Ankou](https://forum.kirupa.com/u/Ankou)\
**Post date:** [April 8, 2006, 10:42pm UTC](https://forum.kirupa.com/t/time-and-date-formatting-help/184895/2 "2006-04-08T22:42:27Z")

</div>

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
<?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. 🙂

```php
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)));
}

```

---

<div class="post-metadata">

**Author:** ![Voetsjoeba](https://avatars.discourse-cdn.com/v4/letter/v/ac91a4/32.png) [@Voetsjoeba](https://forum.kirupa.com/u/Voetsjoeba)\
**Post date:** [April 9, 2006, 8:18am UTC](https://forum.kirupa.com/t/time-and-date-formatting-help/184895/3 "2006-04-09T08:18:52Z")

</div>

Check out this function: [strptime](http://be2.php.net/manual/en/function.strptime.php). 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:

```php
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.

---

<div class="post-metadata">

**Author:** ![mlk](https://avatars.discourse-cdn.com/v4/letter/m/e56c9b/32.png) [@mlk](https://forum.kirupa.com/u/mlk)\
**Post date:** [April 10, 2006, 10:12am UTC](https://forum.kirupa.com/t/time-and-date-formatting-help/184895/4 "2006-04-10T10:12:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Ankou](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/ankou/32/1351_2.png) [@Ankou](https://forum.kirupa.com/u/Ankou)\
**Post date:** [April 10, 2006, 6:38pm UTC](https://forum.kirupa.com/t/time-and-date-formatting-help/184895/5 "2006-04-10T18:38:00Z")

</div>

> [@mlk](#):
>
> My host is usually up to date on php so it should be available.

🙂 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.
