I am recieving a date which uses javascript's date() function to generate a timestamp like so.
1451351941210
However my application is in php and I would like to convert that value to a unix timestamp like below
1451419198
I have tried dividing by 1000 but the function comes back as 18 years not 18 seconds.
Here is the code below
<?php
// Get javascript date() value
$date = 1451351941210;
// divide by 100 and round
$start_time = round($date/1000);
// run through function and echo
echo time_elapsed_string('@'. $start_time);
// Time function
function time_elapsed_string($datetime, $full = false) {
$now = new DateTime;
$ago = new DateTime($datetime);
$diff = $now->diff($ago);
$diff->w = floor($diff->d / 7);
$diff->d -= $diff->w * 7;
$string = array(
'y' => 'year',
'm' => 'month',
'w' => 'week',
'd' => 'day',
'h' => 'hour',
'i' => 'minute',
's' => 'second',
);
foreach ($string as $k => &$v) {
if ($diff->$k) {
$v = $diff->$k . ' ' . $v . ($diff->$k > 1 ? 's' : '');
} else {
unset($string[$k]);
}
}
if (!$full) $string = array_slice($string, 0, 1);
return $string ? implode(', ', $string) . ' ago' : 'just now';
}
Is there anything to directly convert the date() to a unix timestamp?
I want to use PHP, not javascript
The function I have used is taken from
Converting timestamp to time ago in PHP e.g 1 day ago, 2 days ago...