PHP: check if it passed a week after the timestamp

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 from now?

2) Assuming a β€œfalse” on (1), find out how much more time is left before the mark of the week, rounded to the remaining days and hours.

I know this is pretty simple, but it uses a very specific syntax. Never playing with time calculations before, I would appreciate some recommendations.

Thanks!

+6
php
source share
7 answers
 $time = strtotime('2010-05-10 02:49:30'); $one_week_ago = strtotime('-1 week'); if( $time > $one_week_ago ) { // it 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."; } 
+10
source share

Not strtotime allows you to do such things ...

 $timestamp = strtotime($time); $oneweekago = strtotime("-1 week"); if($oneweekago<=$timestamp) { // it been less than one week $secondsleft = $oneweekago - $timestamp; // ... } 
+6
source share

You can use strptime / strftime (or mysql TIMESTAMP) to parse your time, and then check if it is at least one week from the current (one week = 604800 seconds).

If one week has not passed, you can determine how many seconds are left, from which you can calculate the days and hours.

+1
source share
 SELECT * FROM 'contents' WHERE (WEEK(NOW(), 7) = WEEK('publish_up', 7)) AND YEAR('publish_up') = YEAR(NOW()) 
+1
source share

There are many great features for handling dates. Take a look at this page in the manual http://php.net/manual/en/ref.datetime.php

0
source share

mysql has a datetime feature set . date_add and dated among them.
although counting watches can be tricky.
Anyway, I can’t believe that you will do this comparison using PHP

0
source share

Here you:

 $time = strtotime('2010-05-9 02:49:30'); $oneWeekAgo = time() - (7 * 84600); $hasOneWeekPassed = $time < $oneWeekAgo; if (!$hasOneWeekPassed) { $timeLeft = $time - $oneWeekAgo; } var_dump($hasOneWeekPassed); var_dump($timeLeft); 

Absolutely DO NOT do what Colonel Shrapnel said (calculate it in MySQL). Not only is this completely unnecessary, but it will be at least 20 times slower than pure PHP above.

-3
source share

All Articles