0

So I'm trying to get today's date and time and timezone etc. in php. In other questions on stackoverflow I ended up with this: echo date('D M d Y H:i:s O') and it returns this: Tue May 15 2018 23:08:48 +0200.

But when I use Date() function in JS it returns this: Tue May 15 2018 23:12:55 GMT+0200 (Central Europe Daylight Time).

And this is the problem how can I get same result in PHP as in JS ?? I have no idea, maybe I can just add there (Central Europe Daylight Time) this as text, but it's weird. Can you help me out ?

NOTE: And default timezone is set in PHP. Just like that: date_default_timezone_set("Europe/Prague");

This is that adding text version:

$today = date('D M d Y H:i:s O')." (Central Europe Daylight Time)";
echo $today;

And it returns Tue May 15 2018 23:25:09 +0200 (Central Europe Daylight Time) this.

SenTisso
  • 579
  • 7
  • 18

4 Answers4

2

Try this :

$today = date('D M d Y H:i:s O',strtotime("now"));

It will return the current time of the server (as you said it is : Europe/Prague).

And never rely on the users's time in JS (that can be changed from user to user).

Niv Apo
  • 983
  • 1
  • 9
  • 18
0

To address storing the same date value in each language...

You can create a date in both JavaScript and PHP from a Unix UTC timestamp.

In JavaScript:

var timestamp = new Date().getTime();
var formattedDate = new Date(timestamp);
console.log(formattedDate);

In PHP:

$timestamp = time();
$formattedDate = gmdate("D M d Y H:i:s O", $timestamp);
echo $formattedDate;

In the above code, the JavaScript timestamp and the PHP $timestamp are both expressed as Unix UTC timestamps. You can pass these values freely and consistently between the two languages.

If you store the timestamp in this way as your authoritative data, then you can format the date for output whenever you want, however you want, and in whatever time zone.

I would start there so that you have consistent data.

If you are also interested in formatting per se, that's a separate concern.

Will
  • 6,601
  • 3
  • 31
  • 42
0

To display it like the js version try

echo date('D M d Y H:i:s O') .' '. date_default_timezone_get();

OR

$dt = date_timezone_get(date_create(time()));
echo date('D M d Y H:i:s O') . $dt->getName();

This will print out

Tue May 15 2018 23:36:22 +0200 Europe/Berlin

Shully
  • 78
  • 8
0

In your php file, you can write the current unix timestamp directly to a javascript variable. This will give you a stable starting point (server-side supplied javascript date object) to run your countdown upon.

Code:

<?php
echo date('Y-m-d h:i:s', time()) , "<br>\n";  // purely for demonstration
?>
<script>
var unix = new Date(<?=time()*1000?>).toLocaleString("en-US", {timeZone: "Europe/Prague"}); // convert to milliseconds, not seconds; force timezone
document.write(unix);
</script>

Output:

2018-05-16 04:03:00                       <-- (Unix timestamp)
5/16/2018, 6:03:00 AM                     <-- (American formatted Prague time)

Generated Source Code:

2018-05-16 04:03:00<br>
<script>
var unix = new Date(1526443380000).toLocaleString("en-US", {timeZone: "Europe/Prague"}); // convert to milliseconds, not seconds; force timezone
document.write(unix);
</script>

Some references for further research/understanding...

Convert a Unix timestamp to time in JavaScript
How to initialize javascript date to a particular timezone

mickmackusa
  • 43,625
  • 12
  • 83
  • 136