Jump to content

[SOLVED] $date = today


runnerjp

Recommended Posts

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

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

???

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

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.