Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-11-2007, 11:19 PM   PM User | #1
JustinMs66
New Coder

 
Join Date: May 2006
Posts: 29
Thanks: 0
Thanked 0 Times in 0 Posts
JustinMs66 is an unknown quantity at this point
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?
JustinMs66 is offline   Reply With Quote
Old 07-11-2007, 11:31 PM   PM User | #2
Mwnciau
Regular Coder

 
Join Date: May 2006
Location: Wales
Posts: 820
Thanks: 1
Thanked 82 Times in 79 Posts
Mwnciau is on a distinguished road
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...
Mwnciau is offline   Reply With Quote
Old 07-12-2007, 12:09 AM   PM User | #3
rfresh
Regular Coder

 
Join Date: Jun 2007
Location: Los Angeles
Posts: 545
Thanks: 81
Thanked 5 Times in 5 Posts
rfresh is an unknown quantity at this point
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
rfresh is offline   Reply With Quote
Old 07-12-2007, 03:32 PM   PM User | #4
aedrin
Senior Coder

 
Join Date: Jan 2007
Posts: 1,648
Thanks: 1
Thanked 58 Times in 54 Posts
aedrin will become famous soon enough
Quote:
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.
aedrin is offline   Reply With Quote
Old 07-12-2007, 03:52 PM   PM User | #5
daveyand
New Coder

 
Join Date: Jun 2007
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
daveyand is an unknown quantity at this point
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
daveyand is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:03 AM.


Advertisement
Log in to turn off these ads.