strtotime没有给出结果

时间:2013-03-01 08:53:09

标签: php strtotime

为什么是以下代码

$first_day = strtotime('first day of this month', '2012-06-01');
echo $first_day;

没有返回任何结果?字符串是空的。

3 个答案:

答案 0 :(得分:1)

试试这个:

如果php版本小于5.3

<?php 
$d = date_create();
print date_create($d->format('Y-m-1'))->format('Y-m-d') 
?>

如果是php版本5.3 +

<?php
    $d = new DateTime('2012-06-20');
    $d->modify('first day of this month');
    echo $d->format('jS, F Y');
?>

这是另一种解决方案:

<?php 

echo date('Y-m-d', strtotime( 'first day of '.date('2012-06-01'))); ///time stamp

echo strtotime(date('Y-m-d', strtotime( 'first day of '.date('2012-06-01')))); // formated date

?>

答案 1 :(得分:1)

正如您所说没有得到任何回复,因为strtotime函数中给定的第二个参数不是时间戳。

首先转换为时间戳..

见下面的代码:

$first_day = strtotime('first day of this month', strtotime('2012-06-01'));

答案 2 :(得分:0)

只需使用如下,

$first_day = strtotime('2012-06-01');
echo $first_day;

第一个月的日期

<?php
    echo strtotime(date('Y-m-d', strtotime( 'first day of '.date('2012-06-01')));
?>

如果不起作用,请尝试date_parse_from_format()而不是strtotime()