timestamp(int) to date(string) in php


timestamp(int) to date(string) in php

这是我生成当前日期的代码。

$now= Time();
$dt=date("d", strtotime($now));
echo $dt;

它输出01,因为d-m-Y输出01-01-1970而不是当前日期。

$dt是字符串,$now是整数类型。我应该怎么做才能获得正确的日期并将其作为整数类型。

您不需要将time()转换为strtotime()strtotime()接受日期作为字符串。time()将仅以整数形式返回当前时间。

尝试

$date = date('d', time());

如果您想获得当前日期,可以尝试

$dt=date("d-m-Y", strtotime('today')); //dd-mm-yyyy
echo $dt;

$current_date_in_int = Time(); 
$current_date_in_string = date('d-m-Y', $current_date_in_int); //dd-mm-yyyy
echo $current_date_in_string;