如何返回特定unix-timestamp PHP的一个月开头的unix-timestamp

时间:2012-11-08 12:04:49

标签: php unix unix-timestamp

我需要一个泛型函数,它接受一个日期时间的unix-timestamp表示,并返回一个表示,例如,在该月的第一天午夜。

即。我通过1352376093代表现在(星期四,2012年11月8日12:01:33 GMT)并返回1351728000代表(星期四,2012年11月1日00:00:00 GMT)

2 个答案:

答案 0 :(得分:2)

此代码段应该可以帮助您解决问题。只需从现有日期开始只需一个月和一年,然后将其转换回时间戳。

<?php
     $time = 1352376093;
     $date = date('Y-m-01 00:00:00', $time);
     $result = strtotime($date);

答案 1 :(得分:0)

最终得到了这个功能:

$earliest_issue = db_result( $result, 0 );
$day_of_month_of_earliest_issue = date("d", $earliest_issue);
$hour_of_month_of_earliest_issue = date("h", $earliest_issue);
$minute_of_month_of_earliest_issue = date("i", $earliest_issue);
$seconds_in_month = date("s", $earliest_issue);
$seconds_in_day_of_month = (($day_of_month_of_earliest_issue*24*60*60)-86400);
$seconds_in_hour_of_month = (($hour_of_month_of_earliest_issue*60*60)-3600);
$seconds_in_minute_of_month = (($minute_of_month_of_earliest_issue*60));
$start_time_of_query = ($earliest_issue - $seconds_in_day_of_month - $seconds_in_hour_of_month - $seconds_in_minute_of_month - $seconds_in_month);

return $start_time_of_query;

其中$earliest_issue是开始日期......

如果有人读这篇文章的功能比我希望的更优雅那么请捐款,谢谢!

相关问题