datetime - php "countdown til" and "since time" GMT UTC time function -
i'm working function found this, i'm trying make work gmt utc timestamp:
edit: maybe issue how i'm "converting" user input time gmt...
i doing
$the_user_input_date = strtotime('2011-07-20t01:13:00'); $utctime = gmdate('y-m-d h:i:s',$the_user_input_date);
does gmdate('y-m-d h:i:s',$the_user_input_date);
not "convert" gmt? format it? maybe thats issue.
here's times can supply like:
//local time in gmt 2011-07-20t01:13:00 //future time in gmt 2011-07-20t19:49:39
i'm trying work like:
started 36 mins ago start in 33 mins start in 6 hrs 21 mins start in 4 days 4 hrs 33 mins
here's im working far:
edit: new php code im working with, seems add 10 hours on date. ideas? updated here: function ago($from) { $to = time(); $to = (($to === null) ? (time()) : ($to)); $to = ((is_int($to)) ? ($to) : (strtotime($to))); $from = ((is_int($from)) ? ($from) : (strtotime($from))); $units = array ( "year" => 29030400, // seconds in year (12 months) "month" => 2419200, // seconds in month (4 weeks) "week" => 604800, // seconds in week (7 days) "day" => 86400, // seconds in day (24 hours) "hour" => 3600, // seconds in hour (60 minutes) "minute" => 60, // seconds in minute (60 seconds) "second" => 1 // 1 second ); $diff = abs($from - $to); $suffix = (($from > $to) ? ("from now") : ("ago")); foreach($units $unit => $mult) if($diff >= $mult) { $and = (($mult != 1) ? ("") : ("and ")); $output .= ", ".$and.intval($diff / $mult)." ".$unit.((intval($diff / $mult) == 1) ? ("") : ("s")); $diff -= intval($diff / $mult) * $mult; } $output .= " ".$suffix; $output = substr($output, strlen(", ")); return $output; }
@jason
i tried suggested here:
function ago($dateto) { $datetime1 = new datetime( $dateto); $datetime2 = new datetime(); $interval = $datetime1->diff($datetime2); // print_r($interval); $format = ''; if ($interval->h) { $format .= ' %h ' . ($interval->h == 1 ? 'hour' : 'hours'); } if ($interval->i) { $format .= ' %i ' . ($interval->i == 1 ? 'minute' : 'minutes'); } // more logic each interval if ($format) { echo $interval->format($format), ' ago'; } else { echo 'now'; } }
it seems add 10 hours on time.
any ideas going on?
maybe error lies how i'm saving target time? when submits time converted , stored this
the user submitted time start out looking local time: 07/20/2011 11:00 pm
then:
$time = mysql_real_escape_string($_post['time']); $the_date = strtotime($time); //make user input time gmt time $utctime = gmdate('y/m/d h:i:s',$the_date); $query = "insert $table (time) values ('$utctime');"; mysql_query($query);
provided have access php >= 5.3 i'd recommend datetime::diff()
. dateinterval
returned gives parts need display has own methods, such format()
.
here's sample give idea. there more complete samples in comments of php documentation links.
<?php $datetime1 = new datetime('2011-07-20'); $datetime2 = new datetime(); $interval = $datetime1->diff($datetime2); // print_r($interval); $format = ''; if ($interval->h) { $format .= ' %h ' . ($interval->h == 1 ? 'hour' : 'hours'); } if ($interval->i) { $format .= ' %i ' . ($interval->i == 1 ? 'minute' : 'minutes'); } // more logic each interval if ($format) { echo $interval->format($format), ' ago'; } else { echo 'now'; }
it outputs (on system):
22 hours 10 minutes ago
Comments
Post a Comment