time() subtraction

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

time() subtraction

Post by Illusionist »

Would this be the best way to determine if one date is 3 or more days older than today?

Code: Select all

$time = "1090492366";

echo date("n/j/y H:i:s",$time-date("Z"))."<br>";
echo date("n/j/y H:i:s",time()-date("Z"))."<br>";

$startTime= date("ynjHis",$time-date("Z"));
$today = date("ynjHis",time()-date("Z"));

if($today - $startTime>= 3000000){
	echo "over 3 days old";
}
The way the dates are inserted into the database is by the time() function, so this is the first thing I've put together and tested to check, but I was wondering if this would be sufficient or if there was something else I should do....
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

<?php

$time = "1090492366";
$now = time();

echo '$time (' . date('d M Y H:i:s',$time) . ') is ' . ((int)$time < ($now - 3 * 24 * 60 * 60) ? '' : 'not ') . 'older than 3 days from ' . date('d M Y H:i:s',$now);

?>
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Post by ol4pr0 »

Code: Select all

#nice aswell. 
$now = time();
$later = strtotime('Thursday',$now);
$before = strtotime('3 days ago',$now);
print strftime("now: %c \n", $now);
print strftime("later: %c \n", $later);
print strftime("before: %c \n", $before);
feyd

Code: Select all

#whats this called ?! seen it a couple of times want to look into it a bit more.
? '' : 'not '
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

The ternary.. : :arrow: look here
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post by Illusionist »

Thanks feyd, that works alot better.
Post Reply