i have function this:
function time_elapsed_string($ptime) { $date_time = strtotime("1348-10-10 04:30:01") + $ptime; $year = date("y",$date_time); $month = date("m",$date_time); $day = date("d",$date_time); $time = date("h:i:s",$date_time); $etime = time() - $ptime + 1; $a = array( 31536000 => 'year', 2592000 => 'month', 86400 => 'day', 3600 => 'hour', 60 => 'minute', 1 => 'second' ); foreach ($a $secs => $str) { $d = $etime / $secs; if ($d >= 1) { $r = round($d); // ex: return array('date' => $day.'-'.$month.'-'.$year, // 2016-02-20 'time' => $time, // 03:30:04 'difference' => $r . ' ' . $str . ' ago' // 2 month ago ); } } }
and use this:
$ptime = 1470692661; $html = '<span title="date: '.time_elapsed_string($ptime)['date'].' time: '.time_elapsed_string($ptime)['time'].'">in '.time_elapsed_string($ptime)['difference'].'<span>';
as see, i'm using of function's result this:
time_elapsed_string($ptime)['date'] ime_elapsed_string($ptime)['time'] time_elapsed_string($ptime)['difference']
in fact i'm calling function every time need 1 of results. right? or should call once , store array?
note: code works well.
counting time elapsed since date/time mauvais ton.
datetime
has been available since php 5.2.0 , tonns of people underestimate it. why don't use instead of loop
s , if
s?
$create_time = "2016-08-02 12:35:04"; $current_time="2016-08-02 16:16:02"; $dtcurrent = datetime::createfromformat('y-m-d h:i:s', $current_time); // use current timestamp, use following: //$dtcurrent = new datetime(); $dtcreate = datetime::createfromformat('y-m-d h:i:s', $create_time); $diff = $dtcurrent->diff($dtcreate);
now, can format result want:
$interval = $diff->format("%h hours %i minutes %s seconds");
this give clean 3 hours 40 minutes 58 seconds
without arrays, better.
update
there general solution hours / minutes / seconds via regex:
$interval = $diff->format("%y years %m months %d days %h hours %i minutes %s seconds"); // remove 0 values $interval = preg_replace('/(^0| 0) (years|months|days|hours|minutes|seconds)/', '', $interval);
update 2
as of comment:
look, want use approach .. cannot implement .. need 3 things:
time
,date
,difference
..! approach doesn't give me them..
well, know how difference, it's $interval
variable described above.
to time , date, can $dtcreate
variable by, again, using format:
$time = $dtcreate->format('h:i:s'); $date = $dtcreate->format('d-m-y');
Comments
Post a Comment