gpt4 book ai didi

javascript - 在同一文件中将数据从 JavaScript 传递到 php

转载 作者:行者123 更新时间:2023-11-30 08:27:58 25 4
gpt4 key购买 nike

当我在我的 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

因此,您的时区将为“亚洲/达卡”。

关于javascript - 在同一文件中将数据从 JavaScript 传递到 php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42186096/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com