Forum Moderators: coopster
$now = time();
but that gives me the time of the server.
So the question is, is the following the most effictient way to handle the time difference between user and server:
//difference in hours between users and server
$hours_diff = 5;
$secs_diff = $hours_diff * 60 * 60;
$server_time = time();
$user_time = $server_time + $secs_diff;
echo "the time is: " . date("F j, Y, g:i a", $user_time);
This seems a little gerry-rigged to me, and I can see potential problems. For example in my case I am actually recording the time that certain things were done in a database, like when an appointment was added. Do I store the server timestamp or the modified server timestamp plus the offset? What I mean is do I convert the time when displaying the time or when storing the time? So two years from now if another programmer looks at my code, how will he know that the stored timestamp is the orignial server timestamp or the modified user timestamp? If I store the modified stamp and he/she thinks it's the original it could be compensated twice for the offset. And as data gets manipulated often, this could happen several times for one piece of data. It seems messy.
If I'm making no sense, ignore me.
I would store the server timestamp, then convert it on display
using strtotime is a little easier than what you have. Consider we have the var $server_time that could come from the db or from a call to time()
$user_time = strtotime("+5 hours", $server_time);
echo "the time is: " . date("F j, Y, g:i a", $user_time);
should work