Revision: 30144
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at August 9, 2010 05:09 by alvincrespo
Initial Code
<?php //===================================================== // REFERENCE //===================================================== //60 Seconds // 1 minute = 60 seconds // 1 month = // 31 days = 2678400 // 30 days = 2592000 // 28 days = 2419200 // 1 hour = 3600 seconds // 1 day = 86400 seconds // 1 week = 604800 seconds // 1 year = 31536000 seconds //===================================================== echo displayago(); function displayago() { $datePublished = mktime(11, 33, 0, 5, 2, 2009); echo 'Date Published: ' . date('l F j, Y ', $datePublished) . ' at '. strftime('%H:%M%p', $datePublished) . '<br/>'; $dateNow = getdate(); $dateNow = mktime($dateNow['hours'], $dateNow['minutes'], $dateNow['seconds'], $dateNow['mon'], $dateNow['mday'], $dateNow['year']); echo 'Todays Date: ' . date('l F j, Y ', $dateNow) . ' at '. strftime('%H:%M%p', $dateNow) . '<br/>'; echo '<br/>'; $totalSeconds = $dateNow - $datePublished; $yearSeconds = 31536000; //31536000 seconds in a year //$monthSeconds = ''; $weekSeconds = 604800; // 604800 seconds in a week $daySeconds = 86400; // 86400 seconds in a day $hourSeconds = 3600; //3600 seconds in a minute $minuteSeconds = 60; //60 seconds $seconds = 1; $result = ''; //calculate total leap years $totalLeapYears = totalLeapYears(date('Y', $datePublished),$dateNow['year']); //calculate years $years = round($totalSeconds/$yearSeconds); if($years >= 1) { if($years == 1) { $result .= $years . ' <b>year</b> '; } else { $result .= $years . ' <b>years</b> '; } } else { $result .= '0 <b>years</b> '; } //calculate months $months = round(); //calculate weeks //calculate days $days = round($totalSeconds/$daySeconds); if($days >= 365) { $days = $days - 365; } if($days >= 1) { if(round($days) == 1) { $result .= round($days) . ' <b>day</b> '; } else { $result .= round($days) . ' <b>days</b> '; } } else { $result .= '0 <b>days</b> '; } //calculate hours $hours = round($totalSeconds/$hourSeconds); if($hours >= 1) { if($hours == 1) { $result .= $hours . ' <b>hour</b> '; } else { $result .= $hours . ' <b>hours</b> '; } } else { $result .= '0 <b>hours</b> '; } //calculate minutes $minutes = round($totalSeconds/$minuteSeconds); if($minutes >= 1) { if($minutes == 1) { $result .= $minutes . ' <b>minute</b> '; } else { $result .= $minutes . ' <b>minutes</b> '; } } else { $result .= '0 <b>minutes</b> '; } //calculate seconds $seconds = $totalSeconds/$seconds; if($seconds >= 1) { if($seconds == 1) { $result .= $seconds . ' <b>second</b> '; } else { $result .= $seconds . ' <b>seconds</b> '; } } else { $result .= '0 <b>seconds</b> '; } $result .= ' ago'; return $result; } //===================================================================== // UTILITY FUNCTIONS //===================================================================== /* Returns the total amount of years between a certain amount of years. @return */ function totalLeapYears($startYear, $endYear) { $result = 0; for($i = $startYear; $i < $endYear; $i++) { if(date('L', strtotime("$i-01-01"))) { $result += 1; } } return $result; } /* Returns the total amount of days per year, checking for leap year @return */ function getTotalDays($isleapyear = false) { if($isleapyear) { return 366; } return 365; } /* Returns the total amount of days per month, checking for leap year as well (February) @return */ function getDays($month, $isleapyear = false) { switch($month) { case 'January': return 31; break; case 'February': if($isleapyear) { //is leap year return 29; } else { //is not leap year return 28; } break; case 'March': return 31; break; case 'April': return 30; break; case 'May': return 31; break; case 'June': return 30; break; case 'July': return 31; break; case 'August': return 31; break; case 'September': return 30; break; case 'October': return 31; break; case 'November': return 30; break; case 'December': return 31; break; default: break; } } //===================================================================== ?>
Initial URL
Initial Description
Working on creating a 'days ago' script. I know some already exist, but I just want to know how to do it myself. I'm also incorporating leap year functionality and some utility functions that could be used on other projects.
Initial Title
PHP Days Ago Script
Initial Tags
php, date, script
Initial Language
PHP