My php or Mysqli time / timestamps seem out? - php

The code is
$la=time()-$row['laston'];
$unit="secs";
if($la >= 60)
{
$la=(int) ($la/60);
$unit="mins";
}
if($la >= 60)
{
$la=(int) ($la/60);
$unit="hours";
if($la >= 24)
{
$la=(int) ($la/24);
$unit="days";
}
print "<li><a class=\"button\" href=\"user.php?p={$row['uid']]'}\">{$row['display_name']} ~($la $unit)</a></li>";
and $row['laston']; is stored like: 1392490566
My problem is it is showing like:
User1 ~(-58secs) when i refresh the page, I'd expect it to be 0-2 secs not a negative value?
Could someone please help me understand what is causing this?
and would changing $la=time()-$row['laston']; to $la=time()-$row['laston']+58; be an appropriate fix?

Try this:
$la = time() - $row['laston'];
if($la > 0) {
$newLa = $la / 24;
$unit = "days";
} if($la < 84600) {
$newLa = $la / 60;
$unit = "hours";
} if($la < 3600) {
$newLa = $la / 60;
$unit = "mins";
} if($la < 60) {
$newLa = $la;
$unit = "secs";
}
echo round($newLa, 2) . ' ' . $unit;

Related

Minutes and day time operation in php

I have to complete 540 mins i.e 9 hours 00 minutes for today and my today in time is 11:10 AM, So I can go after 6.10pm ie my out time
function convert($time, $format = '%d:%d') {
settype($time, 'integer');
if ($time < 1) {
return;
}
$hours = floor($time / 60);
$minutes = ($time % 60);
return sprintf($format, $hours, $minutes);
}
$remain_min = 540;
$remain_time = convert($remain_min, '%02d hours %02d minutes');
echo 'You have to complete '.$remain_time.' for this week. ';
$in_timeh =11;
$in_timem = 10;
$timeformat = 'AM';
echo "Your in time" . $in_timeh. ":" . $in_timem . $timeformat . "<br />";
How to calculate it ?
I have tried this, but not seems to good, plz anyone help with better suggetion
if($timeformat == "pm"){
$in_timeh += 12;
}
$in_time_minutes = ($in_timeh * 60) + $in_timem;
$total_minutes_today = $remain_min + $in_time_minutes;
$total_minutes_today1 = floor($total_minutes_today/60).":".($total_minutes_today%60);
$newDateTime = date('h:i A', strtotime($total_minutes_today1));
echo "You can go after" . $newDateTime;
Firstly make your both in-time and out-time to time to string, and then passon that values to the following function, it will return to you the difference time.
function timeBetween($start_date,$end_date)
{
$diff = $end_date-$start_date;
$seconds = 0;
$hours = 0;
$minutes = 0;
if($diff % 86400 <= 0){$days = $diff / 86400;} // 86,400 seconds in a day
if($diff % 86400 > 0)
{
$rest = ($diff % 86400);
$days = ($diff - $rest) / 86400;
if($rest % 3600 > 0)
{
$rest1 = ($rest % 3600);
$hours = ($rest - $rest1) / 3600;
if($rest1 % 60 > 0)
{
$rest2 = ($rest1 % 60);
$minutes = ($rest1 - $rest2) / 60;
$seconds = $rest2;
}
else{$minutes = $rest1 / 60;}
}
else{$hours = $rest / 3600;}
}
if($days > 0){$days = $days.' days, ';}
else{$days = false;}
if($hours > 0){$hours = $hours.' hours, ';}
else{$hours = false;}
if($minutes > 0){$minutes = $minutes.' minutes, ';}
else{$minutes = false;}
$seconds = $seconds.' seconds';
return $days.''.$hours.''.$minutes.''.$seconds;
}
try this
$date=date_create("09:00");
date_add($date,date_interval_create_from_date_string("540 minutes"));
echo date_format($date,"H:i");
You can use
$add = date("H:i:s", strtotime('+9 hours'));
echo "You can go after".$add;
I hope this helps you.
<?php
$mystartTime = "11:10:02 AM"; //hour: minute:seconds
$hrs = 60 * 60 * 9;
$mystartTimeSecs = strtotime($mystartTime);
$outTime = date('h:i:s A', $mystartTimeSecs + $hrs);
echo "You can go at " . $outTime;
echo "<br />";
$left = $mystartTimeSecs + $hrs;
$remainingTime = $left - time();
$hours = floor($remainingTime / 3600);
$minutes = floor(($remainingTime / 60) % 60);
$seconds = $remainingTime % 60;
echo "<br />";
echo "Time Left: $hours $minutes $seconds";
echo "<br />";
echo "Time Left: " . date("h:i:s", $remainingTime);
?>

PHP Convert from strtotime into time

I need to add multiple time values as in Hours:mins, so I use
strtotime($value1) + strtotime($value2)
to add all of them, how do I put them back as hours:mins ?
cant use
date("h:i")
it only works if hours < 24.
I appreciate your help. Thanks
Here is an function that will sum all your time values in format HH:MM:
function sum_time() {
$i = 0;
foreach (func_get_args() as $time) {
sscanf($time, '%d:%d', $hour, $min);
$i += $hour * 60 + $min;
}
if ($h = floor($i / 60)) {
$i %= 60;
}
return sprintf('%02d:%02d', $h, $i);
}
// use example
echo sum_time('01:05', '00:02', '05:59'); # 07:06
demo
Try this :
function time_convert($s) {
$m = 0; $hr = 0; $td = "now";
if ($s > 59) {
$m = (int)($s/60);
$s = $s-($m*60); // sec left over
$td = "$m min";
}
if ($m > 59) {
$hr = (int)($m / 60);
$m = $m - ($hr*60); // min left over
$td = "$hr hr";
if ($hr > 1) {
$td .= "s";
}
if ($m > 0) {
$td .= ", $m min";
}
}
return $td;
}
And use it:
$time = (int) strtotime($v1) + strtotime($v2);
echo time_convert($time);
May it helps
The function strtotime() returns the time in seconds since January 1 1970 00:00:00 UTC. So adding the return value of this function might not do what you would expect.
Instead of using the date functions we can manipulate the string and perform some basic arithmetic operations:
<?php
$value1 = "12:44";
$value2 = "13:47";
$arr1 = explode(':', $value1);
$arr2 = explode(':', $value2);
$totalMinutes = (int)$arr1[0] * 60 + (int)$arr1[1] + (int)$arr2[0] * 60 + (int)$arr2[1];
$hours = (int) ($totalMinutes / 60);
$minutes = $totalMinutes % 60; // Modulus: remainder when dividing with 60
echo $hours . ':' . $minutes;
?>
Another way with DateTime
$dt1 = new DateTime($value1);
$dt2 = new DateTime($value2);
$interval = $dt1->diff($dt2);
echo $interval->format('%a day(s) %h hour(s) %i minute(s)') . '<br />';
echo ($interval->format('%a') * 24 + $interval->format('%h')) . ' hour(s) ';
echo $interval->format('%i minute(s)');

Trouble with refreshing countdown in javascript

I have countdown on my site which fails to refresh itself.
Code: http://pastebin.com/DNjb0WSN
window.onload = function() {
idElement = "time";
document.getElementById(idElement).innerHTML =
"<?php countdown(21,00,00,9,15,2012) ?>";
setInterval("document.getElementById(idElement).innerHTML =
'<?php countdown(21,00,00,9,15,2012) ?>'", 1000);
};
// ...
<?php
function countdown($godzina, $minuta, $sekunda, $miesiac, $dzien, $rok) {
$target = mktime($godzina, $minuta, $sekunda, $miesiac, $dzien, $rok);
$now = time();
$sekundy = ($target - $now);
// $sekundy =(int) ($sekundy) ;
$check = 0;
if ($sekundy > 0) {
if ($check == 0) {
if ($sekundy <= 10000) {
// zmien klase
$check = 1;
}
}
$hours = floor($sekundy / 3600);
$minutes = floor(($sekundy / 60) - ($hours * 60));
$seconds = floor(($sekundy) - ($hours * 3600) - ($minutes * 60));
if ($hours < 10) $hours = '0'.$hours;
if ($minutes < 10) $minutes = '0'.$minutes;
if ($seconds < 10) $seconds = '0'.$seconds;
$all = $hours." : ".$minutes." : ".$seconds;
echo $all;
}
else {
echo "Aukcja zakończona!";
}
}
?>
Why it doesn't work?
Because the countdown is not implemented in javascript but php. If you need the countdown to work on the client you need to implement it in javascript or poll the server(NOT RECOMMENDED).
The web is stateless, does that ring any bells?
Try using a countdown library written in JavaScript and parametrize it from PHP:
http://www.hashemian.com/tools/javascript-countdown.htm

PHP ceil returning a float

I've created a function to return the difference between two dates
<?php
class days {
function dateDiff($start, $end) {
$start_ts = strtotime($start);
$end_ts = strtotime($end);
$diff = $end_ts - $start_ts;
$diff1 = ceil($diff / 86400);
return $diff1;
}
}
I have this code in the view :
<?php
$a = new days();
$days = $a->dateDiff($v[17], date('Y/m/d'));
if ($days < 30) {
$ds = $days;
$tm = 'days';
} else {
if ($days < 365) {
$ds = $days / 30;
$tm = 'months';
} else {
$ds = $days / 365;
$tm = 'years';
}
}
$v[17] is the date returned from the database to the view.
When I enter for instance an article in august 2011... It will display :
2.9666666666667 months ago
I ask myself ... How this Ceil method could not return an int value as it's supposed to do?
if that's normal, then what's the solution?
Thank you in advance :)
The ceil funciton works just fine when it returns the number of days.
But the problem is here:
if ($days<365){
$ds=$days/30;
$tm='months';
}
You didn't use ceil this time! You should try something like $ds = ceil($days / 30);.
Same thing for the number of years.
It would probably be more precise to use round instead of ceil, so that 32 days don't translate in 2 months:
$days = $a->dateDiff('10 oct 2011',date('Y/m/d'));
if ($days < 30) {
$ds = $days;
$tm = 'day';
}
else {
if ($days < 365){
$ds = round($days / 30);
$tm = 'month';
}
else {
$ds = round($days / 365);
$tm = 'year';
}
}
if ($ds > 1) {
$tm .= 's';
}
echo "$ds $tm"; # => 1 month; or 2 months using ceil function

Formatting Duration (time) in PHP

Outputting from a directions api I have a duration it will take the user to get from a to b. At the moment it is minutes but if the users journey will take 3 hours and 20 minutes it will output 200 minutes.
I would like it to work out that that is greater than 60 minutes. then divide by 60 and add the remainder to give
3 hours 20 minutes.
How do we do this.
Marvellous
with php >= 5.3.0 you could do that :
$dt = new DateTime();
$dt->add(new DateInterval('PT200M'));
$interval = $dt->diff(new DateTime());
echo $interval->format('%Hh %Im %Ss');
Output (on my locale) : 02h 40m 00s
source : http://php.net/manual/en/class.dateinterval.php
function getNiceDuration($durationInSeconds) {
$duration = '';
$days = floor($durationInSeconds / 86400);
$durationInSeconds -= $days * 86400;
$hours = floor($durationInSeconds / 3600);
$durationInSeconds -= $hours * 3600;
$minutes = floor($durationInSeconds / 60);
$seconds = $durationInSeconds - $minutes * 60;
if($days > 0) {
$duration .= $days . ' days';
}
if($hours > 0) {
$duration .= ' ' . $hours . ' hours';
}
if($minutes > 0) {
$duration .= ' ' . $minutes . ' minutes';
}
if($seconds > 0) {
$duration .= ' ' . $seconds . ' seconds';
}
return $duration;
}
In case you have duration in seconds, you can format it with PHP gmdate() function:
echo gmdate("H:i:s", $seconds);
(Note: works for durations up to 24 hours)
$minutes = 200;
if ($minutes >= 60)
{
$hours = (int)($minutes / 60);
$minutes = $minutes % 60;
}
<?php
$time = 200; // minutes
if ($time > 60) {
$minutes = $time % 60;
$hours = ($time - $minutes) / 60;
}
echo "$hours hours $minutes minutes";
?>
Use modulo division :)
function format_minutes($value)
{
$hours = intval($value / 60);
$minutes = $value % 60;
if ($hours != 0) {
$str = $hours . ' hour';
// Handle pluralisation.
if (abs($hours) != 1) {
$str .= 's';
}
}
// Always show minutes if there are no hours.
if ($minutes != 0 || $hours == 0) {
$str .= ' ' . $minutes . ' minute';
// Handle pluralisation.
if (abs($minutes) != 1) {
$str .= 's';
}
}
// There will be a leading space if hours is zero.
return trim($str);
}
Thanks for the function. I needed to change it to make it work better:
The minutes were over 60:
$seconds = $secs % 60;
$hours = floor($secs / 3600);
$secs = $secs - $hours*3600;
$minutes = floor($secs / 60);
Best
For those who need it... Minutes to Period of Time - PT:
<?php
function pttime($time, $format)
{
if ($time < 1) {
return;
}
$hours = floor($time / 60);
$minutes = ($time % 60);
//is PT
if ($format == 'PT') {
//full hour
if (($hours > 0) && ($minutes == 0)) {
$time_result = 'PT' . $hours . 'H';
}
//hour and minutes
if (($hours > 0) && ($minutes <> 0)) {
$time_result = 'PT' . $hours . 'H' . $minutes . 'M';
}
//just minutes
if ($hours == 0) {
$time_result = 'PT' . $minutes . 'M';
}
}
//it's not PT
else {
$time_result = sprintf("%02s", $hours) . ':' . sprintf("%02s", $minutes);
}
return $time_result;
}
//input in minutes and its outputs
echo pttime(155,'PT'); //output -> PT2H35M
echo pttime(52,'PT'); //output -> PT52M
echo pttime(60,'PT'); //output -> PT1H
echo pttime(60,''); //output -> 01:00
A simple duration-to-formatted-time example (with the duration given in seconds, and the output formatted as [##:]##:##):
public function format_duration($secs, $delimiter = ':')
{
$seconds = $secs % 60;
$minutes = floor($secs / 60);
$hours = floor($secs / 3600);
$seconds = str_pad($seconds, 2, "0", STR_PAD_LEFT);
$minutes = str_pad($minutes, 2, "0", STR_PAD_LEFT).$delimiter;
$hours = ($hours > 0) ? str_pad($hours, 2, "0", STR_PAD_LEFT).$delimiter : '';
return "$hours$minutes$seconds";
}

Categories