Jump to content

Today and Yesterday


mikeroq

Recommended Posts

Im trying to make a today and yesterday function for unix time stamps, i did one but i made it very wrong, it works, it just doesnt work right.

I though of taking off 84600 or 86400 w/e it is, but then that presents a problem cause then it would be a rolling date, i need a function that will do this.


Thanks
Link to comment
https://forums.phpfreaks.com/topic/5210-today-and-yesterday/
Share on other sites

  • 2 weeks later...
ok this no longer works, this is my code
[code]
function nixtime($unix)
    {  
        if ($unix == "1129679173")
            {
                return "Never";
            }
        else {
        $unix = $unix;
          $today = strtotime("today");
        $yesterday = strtotime("yesterday");
        $two = strtotime("2 days ago");
        if ($unix < $today && $unix > $yesterday)
        {          
        $visit = date('g:i a', $unix );
        return "Today, $visit";
        }
        if ($unix > $two && $unix < $yesterday)
        {          
        $visit = date('g:i a', $unix );
        return "Yesterday, $visit";
        }
        else {        
        $visit = date('n/j/y g:i a', $unix );
        return $visit;
        }
           }
      }
[/code]

[a href=\"http://mikeroq.be/?x=forum\" target=\"_blank\"]http://mikeroq.be/?x=forum[/a]

or anywhere else, it willl say yesterday for today, and today for yesterday and show incorrect times like today at 6:30am when its only 4:30am server time
Link to comment
https://forums.phpfreaks.com/topic/5210-today-and-yesterday/#findComment-20808
Share on other sites

You are defining today as any time in the last 24 hours and not as times since midnight.

Try
[code]function nixtime($unix)
    {
        if ($unix == "1129679173")
            {
                return "Never";
            }
        else {
            $today = mktime(0,0,0, date('m'), date('d'), date('Y'));
            $yesterday = mktime(0,0,0, date('m'), date('d')-1, date('Y'));

            if ($unix > $today)
                {
                $visit = date('g:i a', $unix );
                return "Today, $visit";
                }
            elseif ($unix > $yesterday && $unix < $today)
                {
                $visit = date('g:i a', $unix );
                return "Yesterday, $visit";
                }
            else {
                $visit = date('n/j/y g:i a', $unix );
                return $visit;
            }
        }
    }[/code]
Link to comment
https://forums.phpfreaks.com/topic/5210-today-and-yesterday/#findComment-20813
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.