在不使用日期功能的情况下在PHP中打印当前年

时间:2014-08-14 07:26:04

标签: php date

是否可以在php中打印当前年份而不使用日期函数或任何字符串函数。任何人都可以帮我解决这个问题

提前致谢

2 个答案:

答案 0 :(得分:2)

当然可以!这真是太愚蠢了 你走了。

<?php
// First off, get the current timestamp. 
//This is every second, leading from 1970-01-01 till now.
$timeStamp = time();
// So there's a theoretically about 31536000 if we're having 365 days 
// and we're not on a leap year.

// However, there hasn't been 365 leap years since 1970, 
// so this shouldn't be a problem
$years = $timeStamp/31536000;
// Now we have the years that has gone by. We simply add them to 1970
$currentYear = $years + 1970;
// Remove the decimals
$year = floor($currentYear);
echo $year . "\n";

正如您所看到的,这不是一个完整的证明长期解决方案。我不建议在任何生产代码中使用它。

答案 1 :(得分:0)

您可以改为使用time()

echo 1970 + floor(time() / 60 / 60 / 24 / 365.25);
相关问题