当我将null作为第二个参数传递时,PHP date()返回历元

当我将null作为第二个参数传递时,PHP date()返回历元,php,date,Php,Date,我想知道,当我手动将null作为第二个参数传递给date函数时,为什么PHP会给我一个划时代的时间,你们知道吗 print_r(date('Y-m-d', null)); 结果: 1970-01-01 只需使用date('Y-m-d')而不是date('Y-m-d',null) 因为: string date ( string $format [, int $timestamp = time() ] ) 第二个参数是timestamp,它是可选的,它将自动将time()作为默认值,您不需要

我想知道,当我手动将null作为第二个参数传递给date函数时,为什么PHP会给我一个划时代的时间,你们知道吗

print_r(date('Y-m-d', null));
结果:

1970-01-01
只需使用
date('Y-m-d')
而不是
date('Y-m-d',null)

因为:

string date ( string $format [, int $timestamp = time() ] )
第二个参数是timestamp,它是可选的,它将自动将time()作为默认值,您不需要设置null

如果传递null,它将不接受它,因为它需要时间戳作为值。所以它给了你错误的日期

只要用
date('Y-m-d')
代替
date('Y-m-d',null)

因为:

string date ( string $format [, int $timestamp = time() ] )
第二个参数是timestamp,它是可选的,它将自动将time()作为默认值,您不需要设置null


如果传递null,它将不接受它,因为它需要时间戳作为值。因此它给了你错误的日期

日期函数的第二个参数是可选的时间戳。如果您指定它,它将转换为您请求的日期格式

因为PHP不使用严格的类型,所以它实现了他们所谓的
类型杂耍
,如所述。此原则将导致
null
值变为0,表示Unix时代的时间


一种简单的方法是运行
var\u dump((int)null)

日期函数的第二个参数是可选的时间戳。如果您指定它,它将转换为您请求的日期格式

因为PHP不使用严格的类型,所以它实现了他们所谓的
类型杂耍
,如所述。此原则将导致
null
值变为0,表示Unix时代的时间


一种简单的方法是运行
var\u dump((int)null)

我知道我可以这样做,但我需要在函数中使用它进行测试,因此,手动null。通过传递null,您将使用的时间戳从
time()
更改为0。我知道我可以这样做,但我需要在函数中使用它进行测试,因此,手动null。通过传递null,您将使用的时间戳从
time()
更改为0。