Reputation: 5270
When I use getDate()
in my php file, I always get time that lies 5 hours behind system time. So I use JavaScript getting time from system. My code is below
<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>
It's a php file. Now I wanna use "datatime" in my php code
<?php
//Here I use datetime
?>
how do I use it? Thanks in advance.
Upvotes: 1
Views: 934
Reputation: 4579
Why not set your timezone with date_default_timezone_set()
(it will set the timezone globally) ?
<?php
date_default_timezone_set('Indian/Mauritius');
$date= date('Y-m-d H:i');
echo $date;
?>
Output :
2017-02-12 13:54
Or you could use php DateTime
object :
$date = new DateTime();
$date->setTimeZone(new DateTimeZone("Asia/Dhaka"));
echo $date->format('Y-m-d H:i');
Output :
2017-02-12 16:19
EDIT :
Here is the list of php supported timezones : http://php.net/manual/en/timezones.php
So, your timezone will be "Asia/Dhaka".
Upvotes: 3
Reputation: 51
You can not do that. the php script run in yor sever befor sending to users browser, after this run the browser the javascript code in the users computer.
If all you need is a variable with date, use date() function in php with correct params For example:
$today = date("Ymd");
in defult php return month number in 2 digits.
Upvotes: 2