note 100773 added to function.strtotime
| From: | ash at itsash dot co dot uk | Date: | Fri, 05 Nov 2010 17:24:21 +0000 |
| Subject: | note 100773 added to function.strtotime | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-173788@lists.php.net to get a copy of this message | ||
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));
}
}
?>
----
Server IP: 87.124.126.105
Probable Submitter: 81.137.71.201 (proxied: 81.137.71.201)
----
Manual Page -- http://www.php.net/manual/en/function.strtotime.php
Edit -- https://master.php.net/note/edit/100773
Del: integrated -- https://master.php.net/note/delete/100773/integrated
Del: useless -- https://master.php.net/note/delete/100773/useless
Del: bad code -- https://master.php.net/note/delete/100773/bad+code
Del: spam -- https://master.php.net/note/delete/100773/spam
Del: non-english -- https://master.php.net/note/delete/100773/non-english
Del: in docs -- https://master.php.net/note/delete/100773/in+docs
Del: other reasons-- https://master.php.net/note/delete/100773
Reject -- https://master.php.net/note/reject/100773
Search -- https://master.php.net/manage/user-notes.php