当我在我的php文件中使用getDate()时,我总是得到比系统时间落后5个小时的时间。所以我使用JavaScript从系统中获取时间。我的代码在下面
<script type="text/javascript">
Date.prototype.today = function () {
return ((this.getDate() < 10)?"0":"") + this.getDate() +"."+(((this.getMonth()+1) < 10)?"0":"") + (this.getMonth()+1) +"."+ this.getFullYear();
}
Date.prototype.timeNow = function () {
return ((this.getHours() < 10)?"0":"") + this.getHours() +":"+ ((this.getMinutes() < 10)?"0":"") + this.getMinutes() +":"+ ((this.getSeconds() < 10)?"0":"") + this.getSeconds();
}
var newDate = new Date();
var datetime = newDate.today() + " " + newDate.timeNow();
document.write(datetime);
</script>这是一个php文件。现在我想在php代码中使用"datatime“。
<?php
//Here I use datetime
?>我该怎么用呢?提前谢谢。
发布于 2017-02-12 09:54:31
为什么不使用date_default_timezone_set()设置您的时区(它将在全球范围内设置时区)?
<?php
date_default_timezone_set('Indian/Mauritius');
$date= date('Y-m-d H:i');
echo $date;
?>产出:
2017-02-12 13:54
或者您可以使用php DateTime对象:
$date = new DateTime();
$date->setTimeZone(new DateTimeZone("Asia/Dhaka"));
echo $date->format('Y-m-d H:i');产出:
2017-02-12 16:19
编辑:
下面是php支持的时区列表:http://php.net/manual/en/timezones.php
所以,您的时区将是"Asia/Dhaka".
发布于 2017-02-12 09:57:25
你不能那样做。php脚本在browser中运行,然后发送给用户浏览器,然后运行用户计算机中的javascript代码。
如果只需要一个带日期的变量,那么在php中使用带有正确参数的 date ()函数,例如:
$today = date("Ymd");在defult php中,返回2位数的月份数。
https://stackoverflow.com/questions/42186096
复制相似问题