...

View Full Version : PHP Date: Yesterday, 2 days ago... etc



JustinMs66
07-11-2007, 11:19 PM
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?

Mwnciau
07-11-2007, 11:31 PM
This is a function (slightly editted) I made before (for MYSQL DATETIME formats, but have editted for most other formats)


function converttime ( $date ){
$datetime = date('Y-m-d H:i:s', strtotime($date));
list ( $date, $time ) = explode ( ' ', $datetime );
$time = substr( $time, 0, 5);
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...

rfresh
07-12-2007, 12:09 AM
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 :)

aedrin
07-12-2007, 03:32 PM
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.



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


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

daveyand
07-12-2007, 03:52 PM
i got given something like this the other day:



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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum