在同一文件中将数据从 JavaScript 传递到 php
Passing Data from JavaScript to php in same file
当我在 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
?>
我该如何使用它?提前致谢。
为什么不使用 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”。
你不能那样做。
php 脚本 运行 在你的服务器中发送给用户浏览器之前,
在这 运行 浏览器之后,用户计算机中的 javascript 代码。
如果您只需要一个带日期的变量,请使用 php 中的 date() 函数和正确的参数 例如:
$today = date("Ymd");
默认 php return 2 位数的月份数。
当我在 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
?>
我该如何使用它?提前致谢。
为什么不使用 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”。
你不能那样做。 php 脚本 运行 在你的服务器中发送给用户浏览器之前, 在这 运行 浏览器之后,用户计算机中的 javascript 代码。
如果您只需要一个带日期的变量,请使用 php 中的 date() 函数和正确的参数 例如:
$today = date("Ymd");
默认 php return 2 位数的月份数。