/ Published in: PHP
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.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
<?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() { $dateNow = mktime($dateNow['hours'], $dateNow['minutes'], $dateNow['seconds'], $dateNow['mon'], $dateNow['mday'], $dateNow['year']); 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 //calculate years 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 //calculate weeks //calculate days if($days >= 365) { $days = $days - 365; } if($days >= 1) { { } else { } } else { $result .= '0 <b>days</b> '; } //calculate hours 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 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++) { { $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; } } //===================================================================== ?>