Hi guys, could somebody tell me if there is a better way of finding out if a date is "Today" or "Yesterday"? Is there anyway to shorten/speed up the code I already have? Thanks $time = $rows['date']; $lastday = date('m/d/Y',$time); $today = mktime(0,0,0,date("m"),date("d"),date("Y")); $yesterday = mktime(0,0,0,date("m"),date("d")-1,date("Y")); $today = date("m/d/Y", $today); $yesterday = date("m/d/Y", $yesterday); if($today == $lastday){$dateposted="Today";} elseif($yesterday == $lastday) {$dateposted="Yesterday";} else {$dateposted = $lastday;} echo $dateposted; PHP:
if you have access to php 5.3 the answer is yes. Please refer to: http://de3.php.net/manual/en/class.datetime.php http://de3.php.net/manual/en/datetime.sub.php 10 days to past: Object oriented style <?php $date = new DateTime('2000-01-20'); $date->sub(new DateInterval('P10D')); echo $date->format('Y-m-d') . "\n"; ?> PHP: Procedural style <?php $date = date_create('2000-01-20'); date_sub($date, date_interval_create_from_date_string('10 days')); echo date_format($date, 'Y-m-d'); ?> PHP:
$time = $rows['date']; $lastday = date('m/d/Y', $time); $today = strtotime("today"); $yesterday = strtotime("yesterday"); $today = date("m/d/Y", $today); $yesterday = date("m/d/Y", $yesterday); if ($today == $lastday) { $dateposted = "Today"; } elseif ($yesterday == $lastday) { $dateposted = "Yesterday"; } else { $dateposted = $lastday; } echo $dateposted; PHP: