note 55478 added to function.date
| From: | thammer at rtccom dot com | Date: | Thu, 04 Aug 2005 16:46:48 +0000 |
| Subject: | note 55478 added to function.date | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-93080@lists.php.net to get a copy of this message | ||
Relative dates (ie, Today, Yesterday, etc..) are missing from the date function. So I wrote this
function to replace date(). It returns Today, Yesterday, or Day of the Week relative to {today} up
to 6 days. Anything older than that, and it will use the formatting you would normally apply to
date(). I hope somebody finds this useful.
<?
//Returns Today,Yesterday, Day of week ('l')
//up to 6 days ago. Otherwise it uses $format
function RelativeDate($format,$epoch) {
$day = date('j',$epoch);
$today = date('j',time());
$yday = date('j',strtotime("-1 day"));
//account for last month's day
switch($day) {
case $today: return "Today"; break;
case $yday: return "Yesterday"; break;
default:
//Look up to one week ago
for($i=1;$i<=6;$i++) {
$pastday = date('j',strtotime("-$i days"));
if($day == $pastday) {
return(date('l',$epoch));
}
}
//If we made it to here, just print the format given
return date($format,$epoch);
}
}
?>
----
Manual Page -- http://www.php.net/manual/en/function.date.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+55478
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+55478&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+55478&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+55478&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+55478&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+55478&report=yes&reason=non-english
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+55478&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+55478&report=yes
Search -- http://master.php.net/manage/user-notes.php