将数据从JavaScript传递到同一文件中的php

时间:2017-02-12 09:43:18

标签: javascript php getdate

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

我该如何使用它?提前谢谢。

2 个答案:

答案 0 :(得分:3)

为什么不用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 ”。

答案 1 :(得分:2)

你做不到。 php脚本在yor服务器上运行,发送给用户浏览器, 在此之后运行浏览器用户计算机中的javascript代码。

如果您只需要一个带日期的变量,请在php中使用 date()函数并使用正确的参数例如:

$today = date("Ymd");

在defult php中返回2位数的月份数。