Jump to content

[SOLVED] need help with time since a unix timestamp' saying "yesterday" after midnight


brad

Recommended Posts

i have a site for my band and i'm ripping off facebook's mini feed idea.

 

i am proficient in PHP, but i just can't get my head around one thing.

 

i have a class with a function called "printBreadTrail" and every time someone updates their profile, joins, comments on a post, comments on a picture etc this breadTrail class i have written logs the time and action as shown in the screenshot below:

 

whodidwhat.jpg

 

this is the code as it is now, and then i'll explain what i want some help with:

 

    	
        $id = $r["id"];
    	$user = $r["userid"];
    	$did = $r["action"];
    	$what = $r["article"];
$time = $r["time"];
$now = time();

	$bT = "<a href='/members/" . $user . "/'>";
	$bT .= $user;
	$bT .= "</a> ";
	$bT .= $did . " ";

	$difference = $now - $time;
	if ($difference >= "84600") {
		$bT .= "<p class='small'>" . date("D jS, g:ia",$time) . "</p><br>";	
	} else {
		$bT .= "<p class='small'>" . date("g:ia",$time) . "</p><br>";
	}	
	echo $bT;
}

 

currently, if the action happened within the last 24 hours (the 84600 bit) it just shows the time. if it's more than that, it will say that days name and also show the time.

 

i want this:

1) if it happened like 3 days ago i want the Day, Day Number, Month and time like Fri 9th Mar, 10:25am (i can code this date format no problem)

2) if it happened WITHIN the last 24 hours but before midnight, i want it to say "Yesterday, 11:55pm"

3) if it happened WITHIN the last 24 hours but after midnight, i want it to just say the time.

 

does this make sense?

 

if not, basically i want to make it so these two:

angelica updated their profile6:50pm

 

brad updated their profile4:22pm

 

would say "yesterday". (within 24hrs but before midnight)

 

i'm sure this will be easyyy for some of you guys :)

 

thanks for any help you can give me.

There are 86,400 seconds in a day.

 

So, if "now" - "registration date/time" is less than 86,400, we fall into the "yesterday" time frame. Now we can look at the hour of the registration in 24 hour format. If the hour is greater than the "now" hour, it's before midnight, if it's less than or equal it's after midnight.

 

Translating the above into PHP:

<?php
$registration_date_time = '3/12/2007 10:00 pm'; // sample for testing
$now = time(); //UNIX timestamp of the current date/time
$regDate = strtotime($registration_date_time); // UNIX timestamp for the registration date/time
$secs_diff = $now - $regDate;
if ($secs_diff > 86400)
    echo date('l, jS F g:i a',$regDate);
else {
    $checkHour = date('G',$regDate);
    $nowHour = date('G',$now);
    if ($checkHour > $nowHour)
       echo 'Yesterday at '. date('g:i a',$regDate);
    else
       echo date('g:i a',$regDate);
}

?>

 

Ken

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.