php如何处理日期,PHP 处理日期的常用方法

/**

* util for date

*/

class DateUtil

{

/**

* @return Date of last monday

*/

public static function lastMonday($date = null)

{

if(!isset($date))

{

$date = date('Y-m-d');

}

$first = 1;

$index = date('w', strtotime($date));//the index of today in this week, sunday is 0, and so on

$d = $index ? $index - $first : 6;

return date('Y-m-d', strtotime($date . '-' . $d . 'days'));

}

/**

* $n Integer

* @return last $n monday date

*/

public static function lastNMondayDates($n = 4)

{

$result = array();

$n = intval($n);

$n = ($n < 1) ? 4 : $n;

$lastMonday = self::lastMonday();

for ($i = $n - 1; $i >= 0; --$i)

{

$result[] = date('Y-m-d', strtotime($lastMonday . '-' . ($i * 7) . 'days'));

}

return $result;

}

/**

* @return date of tomorrow, eg.2013-09-06

*/

public static function tomorrow()

{

return date('Y-m-d', strtotime('+1 day'));

}

/**

* @return date of today, eg.2013-09-06

*/

public static function today()

{

return date('Y-m-d');

}

/**

* Get the date of yesterday

* If $date is special, it will return the day before $date

* @return date of yesterday, eg.2013-09-06

*/

public static function yesterday($date = null)

{

$time = isset($date) ? strtotime($date) : 0;

return date('Y-m-d', $time + strtotime('-1 day'));

}

/**

* Return the week of special date

* 0 means Sunday, 1 means Monday and so one

* @param String $date

* @return Int : the index of today in this week, sunday is 0, and so on

*/

public static function weekOfDate($date)

{

return date('w', strtotime($date));

}

/**

*

* This function used to get a period of date

* @example

* if today is 2013-09-06. $start = 2, $end = 4

* returns array('2013-09-08', '2013-09-09', '2013-09-10')

*

* @param Integer $start

* @param Integer $end

* @param Boolean $isMongoDate: default false, if true, if will return Mongo date array, or it will return php date list

* @return array date list

*/

public static function nextPeriodDate($start, $end, $isMongoDate = false)

{

$result = array();

$start = intval($start);

$end = intval($end);

if($start > $end)

{

return $result;

}

for ($i = $start; $i <= $end; $i++)

{

if($isMongoDate)

{

$date = date('Y-m-d', strtotime('+' . $i . 'day'));

$result[] = new MongoDate(strtotime($date));

}

else

{

$result[] = date('Y-m-d', strtotime('+' . $i . 'day'));

}

}

return $result;

}

/**

* Get the Nth date from today

* @param number $n: 1

* @param String $date: e.g 2013-10-15

* @return String: e.g 2013-10-16

* @example

* if today is 2013-09-06, $n = 2 return 2013-09-08

*/

public static function nextNDate($n, $date = null)

{

if(!isset($date))

{

return date('Y-m-d', strtotime('+' . intval($n) . 'day'));

}

else

{

return date('Y-m-d', strtotime($date) + 60 * 60 * 24 * $n);

}

}

/**

* Get the date of special date's week

* If $date is special, it will compute from $date, or it will compute from current date

* @param int $week: 0 means sunday, 1 means monday and so on

* @param String $date

* @return string

*/

public static function nextWeek($week, $date = null)

{

$weeks = array('sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday');

if(!isset($date))

{

$date = date('Y-m-d');

}

return date('Y-m-d', strtotime('next ' . $weeks[$week] , strtotime($date)));

}

/**

* Get the date of special week of special month

* @example

* If $weekIndex is 2,$week is 0, $monthIndex is 0, the function will return the date of second Sunday of this month

* If $weekIndex is 1,$week is 1, $monthIndex is 1, the function will return the date of first Monday of next month

* Notice:$monthIndex range is 0-11, if $monthIndex is not in this rang, it will return '1970-01-01'

* @param int $weekIndex: 0 means first week, 1 means second week and so on

* @param int $week: 0 means sunday, 1 means monday and so on

* @param int $monthIndex: 0 means this monthday, 1 means next month and so on

* @return String

*/

public static function dateOfMonthWeek($weekIndex, $week, $monthIndex = 0)

{

$weeks = array('sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday');

$weekIndexs = array('first', 'second', 'third', 'fourth');

$monthIndexs = array('this', 'first', 'second', 'third', 'fourth', 'fifth', 'sixth', 'seventh',

'eighth', 'ninth', 'tenth', 'eleventh', 'twelfth');

return date('Y-m-d', strtotime($weekIndexs[$weekIndex] .' '. $weeks[$week] . ' of ' . $monthIndexs[$monthIndex] .' month'));

}

/**

* Get date string from a MongoDate

* @param MongoDate $date

*/

public static function convertMongoDate($date, $format = 'Y-m-d')

{

if(!isset($date))

{

return '';

}

return date($format, $date->sec);

}

/**

* Date validation

* @param string $dateStr

* @return boolean: true means is date format, false means not

*/

public static function isDate($dateStr, $includeTime = false)

{

try

{

$date = new DateTime(trim($dateStr));

}

catch(Exception $e) {

return false;

}

$month = $date->format('m');

$day = $date->format('d');

$year = $date->format('Y');

if(!checkdate($month, $day, $year))

{

return false;

}

if($includeTime)

{

$strArr = explode(' ', $dateStr);

$strTime = explode(':', $strArr[1]);

if(count($strArr) != 2)

{

return false;

}

if(count($strTime) != 3)

{

return false;

}

}

return true;

}

/**

* Get the week of every month for special date

* If $date is null, it will compute by current date

* The return is like 'm-2-1', and it means second Monday every month

* @param String $date

* @return string

*/

public static function getWeekMonthly($date = null)

{

if(!isset($date))

{

$date = date('Y-m-d');

}

$wk_day = date('w', strtotime($date));

$date_now = date('j', strtotime($date));

$wkday_array = array(0, 1, 2, 3, 4, 5, 6);

$cal_result = ceil($date_now / 7);

return 'm-' . $cal_result . '-' . $wkday_array[$wk_day];

}

/**

* Get the weekIndex of every week for special date

* @example:

* If $date is '2013-10-11' and it will return 'w-5', it means Friday of every week

* @param String $date

* @return string

*/

public static function getWeekOfDate($date = null)

{

if(!isset($date))

{

$date = date('Y-m-d');

}

return 'w-' . date('w', strtotime($date));

}

/**

* Get next $n hours time, positive or negative is allowed for $n

* @param number $n

* @param Date $date:optional, default is current time

* @return string

*/

public static function nextNHours($n, $date = null)

{

if(!isset($date))

{

$date = date('Y-m-d H:i:s');

}

return date('Y-m-d H:i:s', strtotime($date) + (60 * 60 * $n));

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值