获取特定月份和年份的开始和结束时间戳php

问题描述:

我想查找一个月中第一天(比如说2010年9月1日和0:00)和一个月中的最后一天的时间戳9月31日23:59)。获取特定月份和年份的开始和结束时间戳php

如果你有这些日期字符串,你可以简单地使用strtotime(),如果你只有部分信息,你可以使用mktime()

然而,月只有30天;)

例子:

$month = 9; 
$year = 2010; 

$first = mktime(0,0,0,$month,1,$year); 
echo date('r', $first); 

$last = mktime(23,59,00,$month+1,0,$year); 
echo date('r', $last); 
+0

我没有这些字符串,通过查询字符串传递的2个参数是月份和年份。从那里我需要处理第一个和最后一个时间戳。 – dotty 2010-09-03 08:26:42

+0

好的,编辑我的回答 – 2010-09-03 08:29:40

+0

这就是我写的,但是我过来fabrik的问题 $ days_in_month = cal_days_in_month(CAL_GREGORIAN,$ month,$ year); – dotty 2010-09-03 08:35:19

也许这是可以做到简单,但你的想法:如果您使用的是PHP

<?php 

$start = mktime(0, 0, 1, $month, 1, $year); 
$end = mktime(23, 59, 00, $month, date('t', $month), $year); 

?> 

5.3(不要用5.2来试试这个,日期解析器在那里的工作方式不同)你可以说的:

<?php 

$date = "2010-05-10 00:00:00"; 

$x = new DateTime($date); 

$x->modify("last day of this month"); 
$x->modify("last second"); 

echo $x->format("Y-m-d H:i:s"); 
// 2010-05-30 23:59:59 
$timestamp = $x->getTimestamp(); 
+1

如果我是对的,OP需要两个时间戳,而不是格式化的日期。 – fabrik 2010-09-03 08:47:21

+0

在getTimestamp中进行编辑,格式就像一个快速粘贴的证明,它可以工作。谢谢 ! – edorian 2010-09-03 09:04:58