Jump to content

Recommended Posts

Hi guys, I need to work out how many minutes there is until the next hour.. Server time that is. I have a cron that updates user information.. And i would like to display a message to say how in many minutes the update will be. I have looked around but cant seam to find any methods of doing this.

 

Do any of you know how? I would be very greatfull.

 

Thank you

Andy

 

Dam guys haha.. As soon as i post i think of a way. Im going to use date() to figure out how many minutes have passed since last hour.. like 31 minutes.. and then take that away from 60 to give me whats left.

 

Sorry for wasting your time. If you know of any other methods though please tell me.

 

Andy

Link to comment
https://forums.phpfreaks.com/topic/68476-solved-how-many-minutes-till-next-hour/
Share on other sites

Use date to get the current minutes and seconds. Then you can take the current minute away from 60 to get the remaining minutes, you can do the same with the seconds too.

Heres an example.

<?php

// get the current minute and second using date
$date = date("i:s");

// explode against the semi-colon to get the minute and seconds separately
list($cur_min, $cur_sec) = explode(':', $date);

// calculate remaining minutes and secounds
$mins_left = ($cur_min == 59) ? 0 : 60 - $cur_min;
$secs_left = 60 - $cur_sec;

// echo the time remaining sentance
echo 'There is ' . ($mins_left)  . ' minutes and ' . $secs_left . ' seconds left until the next hour';
?>

Hey Wildteen, Thanks for the quick reply. I am going to use this method :)

 

One question though.. I have seen code .. Like yours.. Using "?" and other in brackets.

 

Would you be able to explain the line:

<?php
$mins_left = ($cur_min == 59) ? 0 : 60 - $cur_min;
?>

To me please.. Why are you using comparrison operators ( the ==) on  59 and the ? puts me off. I have seen code like this around.. But never understood it.

 

Andy

Its called a ternary operator . Its an inline version of an if/else statement. The quoted line you posted above basically means this:

if($cur_min == 59)
{
    $mins_left = 0; // true
}
else
{
    $mins_left = 60 - $cur_min // false
}

 

format of the ternary operator:

(expression) ? TRUE : FALSE

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.