note 100773 deleted from function.strtotime by derick

From: Date: Sun, 03 Nov 2013 15:19:39 +0000
Subject: note 100773 deleted from function.strtotime by derick
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-197403@lists.php.net to get a copy of this message
Note Submitter: ash at itsash dot co dot uk ---- I find myself using strtotime() 90% of the time when working with date formats. However there are times when strtotime() doesn't work how I want it to. I have created a DateTime class which contains a couple of useful methods feel free to use it. Please remember as of PHP 5. there is a standard class called "DateTime", thus why my class is called PHPDateTime and it just simply extends PHP's DateTime class with some public static methods. If you are not using php 5, just replce "final class PHPDateTime extends DateTime" with "final class DateTime". I assume you all know the double colon scope? for those whom don't it's simply. <?php // this will give us 2010-02-28 () echo PHPDateTime::DateNextMonth(strftime('%F', strtotime("2010-01-31 00:00:00")), 31); ?> The class is as follows.... <?php /** * IA FrameWork * @package: Classes & Object Oriented Programming * @subpackage: Date & Time Manipulation * @author: ItsAsh <ash at itsash dot co dot uk> */ final class PHPDateTime extends DateTime { // Public Methods // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /** * Calculate time difference between two dates * ... */ public static function TimeDifference($date1, $date2) $date1 = is_int($date1) ? $date1 : strtotime($date1); $date2 = is_int($date2) ? $date2 : strtotime($date2); if (($date1 !== false) && ($date2 !== false)) { if ($date2 >= $date1) { $diff = ($date2 - $date1); if ($days = intval((floor($diff / 86400)))) $diff %= 86400; if ($hours = intval((floor($diff / 3600)))) $diff %= 3600; if ($minutes = intval((floor($diff / 60)))) $diff %= 60; return array($days, $hours, $minutes, intval($diff)); } } return false; } /** * Formatted time difference between two dates * * ... */ public static function StringTimeDifference($date1, $date2) { $i = array(); list($d, $h, $m, $s) = (array) self::TimeDifference($date1, $date2); if ($d > 0) $i[] = sprintf('%d Days', $d); if ($h > 0) $i[] = sprintf('%d Hours', $h); if (($d == 0) && ($m > 0)) $i[] = sprintf('%d Minutes', $m); if (($h == 0) && ($s > 0)) $i[] = sprintf('%d Seconds', $s); return count($i) ? implode(' ', $i) : 'Just Now'; } /** * Calculate the date next month * * ... */ public static function DateNextMonth($now, $date = 0) { $mdate = array(0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31); list($y, $m, $d) = explode('-', (is_int($now) ? strftime('%F', $now) : $now)); if ($date) $d = $date; if (++$m == 2) $d = (($y % 4) === 0) ? (($d <= 29) ? $d : 29) : (($d <= 28) ? $d : 28); else $d = ($d <= $mdate[$m]) ? $d : $mdate[$m]; return strftime('%F', mktime(0, 0, 0, $m, $d, $y)); } } ?>

« previous php.notes (#197403) next »