您好,我试图将日期转换为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来获取实时(当前时间),提前谢谢
发布于 2017-03-09 02:19:03
javascript是客户端的,所以你不能从php获取它,time.html只会在用户阅读的时候打印日期(会显示用户的时间)。如果你想用php打印时间,你应该使用php函数date(),这样你就会得到服务器的日期(php是服务器端),例如:
echo "Time: ". date("Y/m/d H:i:s");注意:strtotime()是一个检查字符串或将字符串转换为时间格式的函数
https://stackoverflow.com/questions/42678778
复制相似问题