Hi All
I have googled this until my eyes hurt and I cant seem to find a consistant answer. What I basically would like to do is insert the current date and time into a MySQL table with a coloumn defined as 'datetime' and how to get it back acording to the users timezone.
Currently I am now adding the date into the database using:
Which I understand be be the GMT timezone. I have chosen this route as I would like to create a standard format in my database. This is correct in my timezone GMT(UK).
But when I retreive the date from my database how can I display it back to the user in their current timezone?
I have read many confusing forums now and am totally baffled as further to this I would like to then work out from the date how long ago it was as opposed to when it was and have modified the following script for this. I have added this as I fear this might be linked.
It probably does not help that I keep converting the type but I love this timeAgo function and its works great in my environment.
I appreciate anyone taking the time to read this and/or reply with your comments.
Thanks
James
I have googled this until my eyes hurt and I cant seem to find a consistant answer. What I basically would like to do is insert the current date and time into a MySQL table with a coloumn defined as 'datetime' and how to get it back acording to the users timezone.
Currently I am now adding the date into the database using:
Code:
$datetime = gmdate( 'Y-m-d H:i:s' );
Which I understand be be the GMT timezone. I have chosen this route as I would like to create a standard format in my database. This is correct in my timezone GMT(UK).
But when I retreive the date from my database how can I display it back to the user in their current timezone?
I have read many confusing forums now and am totally baffled as further to this I would like to then work out from the date how long ago it was as opposed to when it was and have modified the following script for this. I have added this as I fear this might be linked.
It probably does not help that I keep converting the type but I love this timeAgo function and its works great in my environment.
Code:
/**
* mysql2timestamp - Converts MySQL datetime to
* a timestamp number format.
*/
function mysql2timestamp($datetime){
$val = explode(" ",$datetime);
$date = explode("-",$val[0]);
$time = explode(":",$val[1]);
return mktime($time[0],$time[1],$time[2],$date[1],$date[2],$date[0]);
}
function timeAgo($datetime, $granularity=2, $format='d M Y @ G:i'){
$timestamp = $this->mysql2timestamp($datetime);
$difference = time() - $timestamp;
if($difference < 0) return '0 seconds ago'; // if difference is lower than zero check server offset
elseif($difference < 864000){ // if difference is over 10 days show normal time form
$periods = array('week' => 604800,'day' => 86400,'hr' => 3600,'min' => 60,'sec' => 1);
$output = '';
foreach($periods as $key => $value){
if($difference >= $value){
$time = round($difference / $value);
$difference %= $value;
$output .= ($output ? ' ' : '').$time.' ';
$output .= (($time > 1 /*&& $key == 'day'*/) ? $key.'s' : $key);
$granularity--;
}
if($granularity == 0) break;
}
return $datetime." - ".($output ? $output : '0 seconds').' ago';
}
else return date($format, $timestamp);
}
I appreciate anyone taking the time to read this and/or reply with your comments.
Thanks
James