Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    May 2006
    Posts
    29
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Arrow PHP Date: Yesterday, 2 days ago... etc

    i am unsure about how to begin this..
    i want to make a function to check if a given date is past midnight. if it is, its "today". if its before then, check to see if its within the past 24 hours midnight. if it is, its yesterday. and 1 more, then 2 days ago.
    i need that to go on until "3 days ago".

    i am unsure about how to go about starting this, can someone help?

  • #2
    Regular Coder
    Join Date
    May 2006
    Location
    Wales
    Posts
    820
    Thanks
    1
    Thanked 82 Times in 79 Posts
    This is a function (slightly editted) I made before (for MYSQL DATETIME formats, but have editted for most other formats)

    PHP Code:
    function converttime $date ){
            
    $datetime date('Y-m-d H:i:s'strtotime($date));
        list ( 
    $date$time ) = explode ' '$datetime );
        
    $time substr$time05);
        list ( 
    $hour$minute ) = explode (':'$time);
        
    $return 'at ' $hour ':' $minute;
        
        list (
    $year$month$day) = explode '-'$date );
        list (
    $year2$month2$day2) = explode (' 'date('Y m d'));
        
        if (
    $year == $year2 && $month == $month2 && $day == $day2){
            return 
    '<span style="font-weight:bolder;">today</span> ' $return;
        }
        if (
    $year == $year2 && $month == $month2 && $day == $day2 1){
            return 
    '<span style="font-weight:bolder;">yesterday</span> ' $return;
        }
    if (
    $year == $year2 && $month == $month2 && $day == $day2 2){
            return 
    '<span style="font-weight:bolder;">two days ago</span> ' $return;
        }
    if (
    $year == $year2 && $month == $month2 && $day == $day2 3){
            return 
    '<span style="font-weight:bolder;">three days ago</span> ' $return;
        }
        
        return 
    $date ' ' $return;

    P.S. probably a much better way though...

  • #3
    Regular Coder
    Join Date
    Jun 2007
    Location
    Los Angeles
    Posts
    545
    Thanks
    81
    Thanked 5 Times in 5 Posts
    While @Mwnciau's code may work I was thinking about using the mktime() function. I don't have any code to offer up but my thinking is along these lines:

    1. Get todays date and hour and convert into the unix timestamp via mktime(). Now you have a baseline to work from.

    2. Take what ever date you wish to check and convert that into a unix timestamp also. Now all you hvae to do is subtract the difference and figure out how far back that date is.

    There are 86400 seconds in the unix timestamp per 24 hour period - so to subtract one day you would simply subtract 86400 from your timestamp value. The hourly value is 3600. So if you did your base line at 6:00am today then 3600 * 6 == 21600 seconds. Let's say your baseline timestamp for today (at 6am) was 1184195100 so subtracting 3600 from that value would give you midnite. I think you can see how that would work.

    On the other hand, maybe we could just go with @Mwnciau's code
    RalphF
    Business Text Messaging Services
    https://www.MobileTextingService.com

  • #4
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    Get todays date and hour and convert into the unix timestamp via mktime(). Now you have a baseline to work from.
    Or just use the result of time()?

    Don't use seconds though. If it's 11 AM, and you go back 12 hours, the difference would be less than 86400 seconds using your idea, so it would show as today. But 11 PM last night is considered yesterday. It's better to use the day.

    PHP Code:
    function timeDiffToString($time) {
      
    $dayToday date('z');
      
    $dayTime date('z'$time);
      if (
    $dayToday == $dayTime) {
        return 
    "Today";
      } else if (
    $dayToday $dayTime) {
        return 
    "Yesterday";
      } else {
        
    // return whatever you want as default
      
    }

    You'd still need to check year, etc. But that's the basic idea.

  • #5
    New Coder
    Join Date
    Jun 2007
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts
    i got given something like this the other day:

    Code:
    static function sectostr($time){
    	    if($time == 0) {
    		return "Today";
    	    }
    
    	    $sec_time=array("century"=>3.1556926*pow(10,9),"decade"=>315569260,"year"=>31556926,"month"=>2629743.83,"week"=>604800,"day"=>86400,"hour"=>3600,"minute"=>60,"second"=>1);
    	    $str="";
    	    foreach($sec_time as $key=>$seconds){
    		    if($seconds > $time)continue;
    		    $current_value=intval($time/$seconds);
    		    if($str != "") {
    			$str .= " ";
    		    }
    		    $str.=$current_value.(($current_value!='1')?" {$key}s ":" {$key}");
    		    $time%=$seconds;
    	    }
    
    	    if($str == "1 day") {
    		return "Yesterday";
    	    }
    
    	    return str_replace("centurys","centuries", $str);
    	}
    Expects a time in seconds.
    PHP / CSS / Perl / XML / XHTML

    For cheap ipod touch go to protella.com


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •