runnerjp Posted November 6, 2008 Share Posted November 6, 2008 currently i have function gettheTime($dbtime,$time) { // Get current timestamp $intTime = time(); // Calculate difference $intDiff = $intTime - $dbtime; // Check time switch(true) { case ($intDiff < 60): echo "<strong>Less than a minute ago</strong>"; break; case ($intDiff < 3600): echo "<strong>Less than an hour ago</strong>"; break; case ($intDiff < 7200): echo "<strong>One hour ago</strong>"; break; case ($intDiff < 10800): echo "<strong>Two hours ago</strong>"; break; case ($intDiff < 86400): echo "<strong>Today</strong>"; break; case ($intDiff < 172800): echo "<strong>Yesterday</strong>"; break; default: echo $time; }} but there is a error in my ways... case ($intDiff < 86400): echo "<strong>Today</strong>"; break; case ($intDiff < 172800): echo "<strong>Yesterday</strong>"; will only get hours passed... if i posted my post last night at 10 oclock it will still say today as 86400 has not passed... how can i fix it so today and yesterday is echoed if 10800 have passed and it is actually today or yesterday Link to comment https://forums.phpfreaks.com/topic/131628-solved-date-today/ Share on other sites More sharing options...
bobbinsbro Posted November 6, 2008 Share Posted November 6, 2008 perhaps you could get the timestamp of the most recent midnight (00:00), and then to check if the post was today, the condition would be: case ($dbtime > $midnight): and the condition for yesterday would be: case ($dbtime > ($midnight - 86400): Link to comment https://forums.phpfreaks.com/topic/131628-solved-date-today/#findComment-683658 Share on other sites More sharing options...
runnerjp Posted November 6, 2008 Author Share Posted November 6, 2008 how would i get timestamp of most recent midnight?? also with my swtich i take it that if 1 does not work it moves to other e.g case ($intDiff < 7200): -- not this one so move onto next case echo "<strong>One hour ago</strong>"; break; case ($intDiff < 10800): echo "<strong>Two hours ago</strong>"; break; Link to comment https://forums.phpfreaks.com/topic/131628-solved-date-today/#findComment-683663 Share on other sites More sharing options...
runnerjp Posted November 6, 2008 Author Share Posted November 6, 2008 could i do something like $today = (date("d/m/Y") case ($intDiff = $today): Link to comment https://forums.phpfreaks.com/topic/131628-solved-date-today/#findComment-683669 Share on other sites More sharing options...
bobbinsbro Posted November 6, 2008 Share Posted November 6, 2008 wouldn't this give you the timestamp of the most recent midnight? $midnight = strtotime(date("d F Y")." 00:00"); why should this interfere with the flow of your switch? ??? Link to comment https://forums.phpfreaks.com/topic/131628-solved-date-today/#findComment-683671 Share on other sites More sharing options...
runnerjp Posted November 6, 2008 Author Share Posted November 6, 2008 yes but the thing is if its 24+ midnight i want it to display yesterday.... if its 48 hours + midnight i want to just show $time Link to comment https://forums.phpfreaks.com/topic/131628-solved-date-today/#findComment-683676 Share on other sites More sharing options...
bobbinsbro Posted November 6, 2008 Share Posted November 6, 2008 ??? it would still work... <?php function gettheTime($dbtime,$time) { // Get current timestamp $intTime = time(); // Calculate difference $intDiff = $intTime - $dbtime; $midnight = strtotime(date("d F Y")." 00:00"); //get the timestamp of last midnight // Check time switch(true) { case ($intDiff < 60): echo "<strong>Less than a minute ago</strong>"; break; case ($intDiff < 3600): echo "<strong>Less than an hour ago</strong>"; break; case ($intDiff < 7200): echo "<strong>One hour ago</strong>"; break; case ($intDiff < 10800): echo "<strong>Two hours ago</strong>"; break; case ($dbtime > $midnight): //if the dbtime is later than the last midnight, dbtime = today echo "<strong>Today</strong>"; break; case ($dbtime > ($midnight - 86400)): //if dbtime is later than midnight yesterday, dbtime = yesterday echo "<strong>Yesterday</strong>"; break; default: //else echo $time. echo $time; }} ?> i changed your code and added comments. if this doesn't work, i must not be understanding what you want to do... :-\ Link to comment https://forums.phpfreaks.com/topic/131628-solved-date-today/#findComment-683690 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.