Let's assume:
$time = '2010-05-17 02:49:30' // (retrieved from MySQL TIMESTAMP field)
How do I do the following in PHP:
1) Check if it has been more than one week since this time has passed?
2) Assuming "false" on (1), find out how much more time until the one week mark, rounded to days and hours remaining.
I know this is pretty straightforward, but it uses a very specific syntax. Having never played with time calculations before, I'd appreciate some guidance.
Thanks!
$time = strtotime('2010-05-10 02:49:30');
$one_week_ago = strtotime('-1 week');
if( $time > $one_week_ago ) {
// it's sooner than one week ago
$time_left = $time - $one_week_ago;
$days_left = floor($time_left / 86400); // 86400 = seconds per day
$hours_left = floor(($time_left - $days_left * 86400) / 3600); // 3600 = seconds per hour
echo "Still $days_left day(s), $hours_left hour(s) to go.";
}
Doesn't strtotime let you do things like this...
$timestamp = strtotime($time);
$oneweekago = strtotime("-1 week");
if($oneweekago<=$timestamp) {
// it's been less than one week
$secondsleft = $oneweekago - $timestamp;
// ...
}
You can use strptime/strftime (or mysql TIMESTAMP) to parse your time and then check if it is at least one week form the present (one week = 604800 seconds).
If one week has not passed then you can work out how many seconds still remain from which you can calculate days and hours left.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With