Jump to content

date compare


jeff5656

Recommended Posts

Whats the code to compare to see if the current date is more than 24 hours old?

would this work? (I didn't include the $query database lines):


$curr_date = date ("Y-m-d");
$today = strtotime ($curr_date);

if ($row['problist_date'] <= $curr_date ....???? 
{
echo "the entry was last edited more than 24 hours ago"
}
else {
echo "entry is up to date";
}

Link to comment
https://forums.phpfreaks.com/topic/131978-date-compare/
Share on other sites

what format is problist_date in? a unix timestamp or a formatted date

for a foramtted date try this

if((strtotime($row['problist_date'])+24*60*60) < time()){
echo "the entry was last edited more than 24 hours ago"
}else{
echo "entry is up to date";
}[code=php:0]
and for a unix timestamp use
[code=php:0]if(($row['problist_date'+24]*60*60) < time()){

by unix timestamp i mean seconds past January 1, 1970

 

Scott.

Link to comment
https://forums.phpfreaks.com/topic/131978-date-compare/#findComment-685773
Share on other sites

i would recommend making it a DATETIME field as if you put in a time at say 2008-11-09 23:59 that would be recorded as 2008-11-09

with is fine but by 2008-11-10 00:01 that code would say it is older than 24 hours even though it has only been 2 mins a DATETIME field would solve that

 

Scott.

Link to comment
https://forums.phpfreaks.com/topic/131978-date-compare/#findComment-685789
Share on other sites

I changed the field from DATE to DATETIME, so how do I update this:

 

$current_date = date ("m/d/y");
$problist_date = date("Y-m-d", strtotime($current_date));

 

Also, how do I deal with fields that don't have an entry yet and therefor are "0000-00-00" in the database?

tnx

Link to comment
https://forums.phpfreaks.com/topic/131978-date-compare/#findComment-685792
Share on other sites

<?php
//Current timestamp
$today=mktime();

//Current timestamp minus 24 hours
$hours24=mktime(date("s"), date("i"), date("H"), date("m"), date("d")-1, date("Y"));

//Timestamp of the date you're comparing
$compare='TIMESTAMP HERE';

//If $today is less that $compare, 24 hours has passed.
if($hours24<$compare && $today>$compare){
echo "24 Hours has passed";
} else if($today>$compare){
echo "The date you are comparing is in the future from now";
} else{
echo "24 Hours has not passed";
}
?>

 

 

Link to comment
https://forums.phpfreaks.com/topic/131978-date-compare/#findComment-685794
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.