您好我尝试将日期转换为strtotime,但我的代码不起作用;
time.html - 显示当前时间2017-03-09 07:11:01
为time.html
<script type="text/javascript">
function getDateTime() {
var now = new Date();
var year = now.getFullYear();
var month = now.getMonth()+1;
var day = now.getDate();
var hour = now.getHours();
var minute = now.getMinutes();
var second = now.getSeconds();
if(month.toString().length == 1) {
var month = '0' + month;
}
if(day.toString().length == 1) {
var day = '0' + day;
}
if(hour.toString().length == 1) {
var hour = '0' + hour;
}
if(minute.toString().length == 1) {
var minute = '0' + minute;
}
if(second.toString().length == 1) {
var second = '0' + second;
}
var dateTime = year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second;
return dateTime;
}
document.write(getDateTime());
</script>
的index.php
$file = file_get_contents("http://localhost/time.html");
$file = strtotime($file);
echo "Time: ". $file;
代码工作方式如下:strtotime(“2017-03-09 07:11:01”);但我需要使用file_get_contents来获取实时(当前时间),提前谢谢
答案 0 :(得分:1)
javascript是客户端的,所以你无法从php获取它,time.html只会打印用户读取它的日期(将显示用户时间)。如果你想在php中打印时间,你应该使用php函数date()
,这样你就可以获得服务器日期(php是服务器端),例如:
echo "Time: ". date("Y/m/d H:i:s");
注意:strtotime()
是检查字符串或将字符串转换为时间格式的函数