Jump to content

[SOLVED] Converting Epoch Format


tereglow

Recommended Posts

Hello,

 

I was wondering if someone could point me in the right direction to do some date conversions.  What I need is to be able to convert a standard date (i.e. 2007-06-01 23:33) to epoch in milliseconds, and vice versa.

 

With Perl, I've used the TimeDate::Format::Epoch CPAN module; am looking for similar functionality in PHP.

 

Best Regards,

Tom

Link to comment
https://forums.phpfreaks.com/topic/54466-solved-converting-epoch-format/
Share on other sites

Hi Tom

 

Try this:

 

<?php
$day    = 6;
$month  = 6;
$year   = 2007;
$hour   = 22;
$minute = 12;
$second = 15;

$timestamp  = mktime($hour, $minute, $second, $month, $day, $year);
$date       = date('Y-m-d H:m', $timestamp);

echo $timestamp . '<br />';
echo $date;
?>

 

If you need microseconds, have a look at http://www.php.net/manual/en/function.microtime.php

Thanks for all of the help.  I was able to do this via the following:

 

<?php
# Convert Epoch in Milliseconds to Seconds
$epoch=1171391881904*.001;

# Convert Epoch in Seconds to an Associate Array using getdate
$t = getdate($epoch);

# Print out the desired format using the elements in the array.
print $t['mon']."/".$t['mday']."/".$t['year']." ".$t['hours'].":".$t['minutes'];
?>

 

Thanks again.

Tom

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.