php时间跨度,在PHP中执行与日期时间相关的操作
小编典典
PHP5
+的DateTime对象很有用,因为它知道leap时间和夏令时,但是它需要一些扩展才能真正解决问题。我写了下面的文章来解决类似的问题。find_WeekdaysFromThisTo()方法是蛮力的,但是,如果您的时间跨度小于2年,则可以相当快速地工作。
$tryme = new Extended_DateTime('2007-8-26');
$newer = new Extended_DateTime('2008-9-1');
print 'Weekdays From '.$tryme->format('Y-m-d').' To '.$newer->format('Y-m-d').': '.$tryme -> find_WeekdaysFromThisTo($newer) ."\n";
/* Output: Weekdays From 2007-08-26 To 2008-09-01: 265 */
print 'All Days From '.$tryme->format('Y-m-d').' To '.$newer->format('Y-m-d').': '.$tryme -> find_AllDaysFromThisTo($newer) ."\n";
/* Output: All Days From 2007-08-26 To 2008-09-01: 371 */
$timefrom = $tryme->find_TimeFromThisTo($newer);
print 'Between '.$tryme->format('Y-m-d').' and '.$newer->format('Y-m-d').' there are '.
$timefrom['years'].' years, '.$timefrom['months'].' months, and '.$timefrom['days'].
' days.'."\n";
/* Output: Between 2007-08-26 and 2008-09-01 there are 1 years, 0 months, and 5 days. */
class Extended_DateTime extends DateTime {
public function find_TimeFromThisTo($newer) {
$timefrom = array('years'=>0,'months'=>0,'days'=>0);
// Clone because we're using modify(), which will destroy the object that was passed in by reference
$testnewer = clone $newer;
$timefrom['years'] = $this->find_YearsFromThisTo($testnewer);
$mod = '-'.$timefrom['years'].' years';
$testnewer -> modify($mod);
$timefrom['months'] = $this->find_MonthsFromThisTo($testnewer);
$mod = '-'.$timefrom['months'].' months';
$testnewer -> modify($mod);
$timefrom['days'] = $this->find_AllDaysFromThisTo($testnewer);
return $timefrom;
} // end function find_TimeFromThisTo
public function find_YearsFromThisTo($newer) {
/*
If the passed is:
not an object, not of class DateTime or one of its children,
or not larger (after) $this
return false
*/
if (!is_object($newer) || !($newer instanceof DateTime) || $newer->format('U') < $this->format('U'))
return FALSE;
$count = 0;
// Clone because we're using modify(), which will destroy the object that was passed in by reference
$testnewer = clone $newer;
$testnewer -> modify ('-1 year');
while ( $this->format('U') < $testnewer->format('U')) {
$count ++;
$testnewer -> modify ('-1 year');
}
return $count;
} // end function find_YearsFromThisTo
public function find_MonthsFromThisTo($newer) {
/*
If the passed is:
not an object, not of class DateTime or one of its children,
or not larger (after) $this
return false
*/
if (!is_object($newer) || !($newer instanceof DateTime) || $newer->format('U') < $this->format('U'))
return FALSE;
$count = 0;
// Clone because we're using modify(), which will destroy the object that was passed in by reference
$testnewer = clone $newer;
$testnewer -> modify ('-1 month');
while ( $this->format('U') < $testnewer->format('U')) {
$count ++;
$testnewer -> modify ('-1 month');
}
return $count;
} // end function find_MonthsFromThisTo
public function find_AllDaysFromThisTo($newer) {
/*
If the passed is:
not an object, not of class DateTime or one of its children,
or not larger (after) $this
return false
*/
if (!is_object($newer) || !($newer instanceof DateTime) || $newer->format('U') < $this->format('U'))
return FALSE;
$count = 0;
// Clone because we're using modify(), which will destroy the object that was passed in by reference
$testnewer = clone $newer;
$testnewer -> modify ('-1 day');
while ( $this->format('U') < $testnewer->format('U')) {
$count ++;
$testnewer -> modify ('-1 day');
}
return $count;
} // end function find_AllDaysFromThisTo
public function find_WeekdaysFromThisTo($newer) {
/*
If the passed is:
not an object, not of class DateTime or one of its children,
or not larger (after) $this
return false
*/
if (!is_object($newer) || !($newer instanceof DateTime) || $newer->format('U') < $this->format('U'))
return FALSE;
$count = 0;
// Clone because we're using modify(), which will destroy the object that was passed in by reference
$testnewer = clone $newer;
$testnewer -> modify ('-1 day');
while ( $this->format('U') < $testnewer->format('U')) {
// If the calculated day is not Sunday or Saturday, count this day
if ($testnewer->format('w') != '0' && $testnewer->format('w') != '6')
$count ++;
$testnewer -> modify ('-1 day');
}
return $count;
} // end function find_WeekdaysFromThisTo
public function set_Day($newday) {
if (is_int($newday) && $newday > 0 && $newday < 32 && checkdate($this->format('m'),$newday,$this->format('Y')))
$this->setDate($this->format('Y'),$this->format('m'),$newday);
} // end function set_Day
public function set_Month($newmonth) {
if (is_int($newmonth) && $newmonth > 0 && $newmonth < 13)
$this->setDate($this->format('Y'),$newmonth,$this->format('d'));
} // end function set_Month
public function set_Year($newyear) {
if (is_int($newyear) && $newyear > 0)
$this->setDate($newyear,$this->format('m'),$this->format('d'));
} // end function set_Year
} // end class Extended_DateTime
2021-03-10
php时间跨度,在PHP中执行与日期时间相关的操作相关推荐
- ef 执行mysql语句_在EF中执行SQL语句
一.为什么要在EF中执行SQL语句 使用EF操作数据库,可以避免写SQL语句,完成使用Linq实现,但为什么还要在EF中执行SQL语句呢.如果要写SQL语句,完全可以使用ADO.NET来操作数据库.这 ...
- 在EF中执行SQL语句
一.为什么要在EF中执行SQL语句 使用EF操作数据库,可以避免写SQL语句,完成使用Linq实现,但为什么还要在EF中执行SQL语句呢.如果要写SQL语句,完全可以使用ADO.NET来操作数据库 ...
- 在 ASP.NET 中执行 URL 重写
在 ASP.NET 中执行 URL 重写 发布日期: 8/23/2004 | 更新日期: 8/23/2004 Scott Mitchell 4GuysFromRolla.com 适用范围: Micro ...
- mysql iso 时间_mysql 中 时间和日期函数
原文链接: mysql 中 时间和日期函数 - redfox - 博客园 http://www.cnblogs.com/redfox241/archive/2009/07/23/1529092.htm ...
- 如何查询mysql中执行效率低的sql语句
一些小技巧 1. 如何查出效率低的语句? 在MySQL下,在启动参数中设置 --log-slow-queries=[文件名],就可以在指定的日志文件中记录执行时间超过long_query_time(缺 ...
- ACCESS中执行sql语句
ACCESS中执行sql语句 简单的说:查询--新建-- 设计视图--选择表或者不选--右键新建SQL视图 不会就看图 access采用sql语句与sql的区别 Access中提供查询对象,在设计时可 ...
- 如何确定恶意软件是否在自己的电脑中执行过?
很不幸,你在自己的电脑里发现了一个恶意的可执行程序!那么问题来了:这个文件到底有没有执行过? 在这篇文章中,我们会将注意力放在Windows操作系统的静态取证分析之上,并跟大家讨论一些能够帮助你回答上 ...
- shell脚本中执行命令_如何在Shell脚本中执行命令?
shell脚本中执行命令 Shell is a command-line interpreter that allows the user to interact with the system. I ...
- 【RHCSA】Linux中执行命令
目录 命令 命令分类 : 选项 : 参数: 补全: 查看命令帮助: 1.查看bash内部命令帮助 : 2.命令 --help : 3.man用来提供在线帮助,使用权限是所有用户. 4.info pag ...
最新文章
- [Beta]第二次 Scrum Meeting
- C++ Primer 5th笔记(chap 19 特殊工具与技术)使用 RTTI
- 基于直方图的图像增强算法(HE、CLAHE、Retinex)
- js小案例:使用location.href自动跳转页面
- QT的QApplication类的使用
- js兼容注意事项--仅供参考
- weblogic jms消息 删除_消息队列与消息中间件概述:消息中间件核心概念与技术选型...
- LwIP应用开发笔记之三:LwIP无操作系统UDP客户端
- 脚本清理maven项目打包残留文件,节省磁盘空间
- ORA-00257: archiver error. Connect internal only, until freed——解决
- IntelliJ IDEA 背景色以及字体设置
- 「代码随想录」本周学习小结!(动态规划系列五)
- further occurrences of HTTP header parsing errors will be logged at DEBUG level.
- 【数学建模】基于matlab GUI停车场仿真系统【含Matlab源码 1046期】
- 用户故事与敏捷方法—编写故事
- java 开根号函数_java如何开根号?
- Flutter笔记(9)flutter中baseline基准线布局
- linux find命令 括号,Linux中find命令细节详解
- 搜狐公司董事局主席兼首席执行官——张朝阳名言4
- 智慧农业智能节水灌溉 机井灌溉控制器