1

This is my first post. I have a problem that i can't seem to resolve. Here it goes:

I have a PHP script that prints the date of an event to the page:

$event_datetime = date("g:i A (m/d/y)", strtotime($row['event_time']));
echo $event_datetime

I want to use Javascript to convert that $event_datetime into client local timezone preferably or local client computer time.

Any ideas ?

Thanks in advance !

fearlex
  • 55
  • 1
  • 8
  • How can that be possible when the date/time string does not include any timezone information whatsoever? Can you tell me what time it is where you live if I tell you that it is 10 am somewhere in the world? – Jon Aug 19 '13 at 18:45
  • Basically the date is just printed out from the database in UTC. My main focus here is to convert a printed or return date from PHP into JS, but somehow getting to show in current time, kind of $returned_date - $localclienttime = ?? Kind of – fearlex Aug 19 '13 at 19:20

3 Answers3

5

Instead of echoing a date to javascript, just echo the timestamp and use javascripts date object.

var date = new Date(<?php echo strtotime($row['event_time']) * 1000); ?>);

You can now use javascripts date object to print out the time however you want. Like this:

date.toLocaleDateString();
//The default output will be the users' timezone (the client where the javascript is being executed), but can be configured with the options argument

See:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date

For more on the javascript date object

crowebird
  • 2,488
  • 1
  • 17
  • 20
2

You can use php to convert all dates into a timezone with:

date_default_timezone_set('America/Los_Angeles');

For example:

$time = time();
$date = date(DATE_RFC822, $time);
echo $date . PHP_EOL;

date_default_timezone_set("Australia/Perth");
$date = date(DATE_RFC822, $time);
echo $date. PHP_EOL;

Output:

Mon, 19 Aug 13 18:49:40 +0000
Tue, 20 Aug 13 02:49:40 +0800

You would just need to get the clients timezone from somewhere (a preference saved in the database or getting it via javascript for example).

See Getting the client's timezone in JavaScript for using javascript

Community
  • 1
  • 1
immulatin
  • 2,118
  • 1
  • 12
  • 13
  • I tried both of these approach but to no avail to what my client needs. Maybe i'm doing the wrong question. Basically, i need to take a date and time, (time being more important) from a mySQL database, and somehow convert that time so the user seeing this admin panel can see events that happened a minute ago, in this format (00:00). So if the event ended at 5:30 in PST, he sees it on his panel at 7:30 EST. Does that makes sense ? Thanks though – fearlex Aug 20 '13 at 06:33
  • Not sure what you mean. If you pull the date out of the database, set the timezone to EST and create a date (after the change of timezone) and then echo it out, the time will be 7:30EST as you say. – immulatin Aug 20 '13 at 14:36
  • Did not see this before posting my own answer, but you were right from the beggining, i'm marking this as the correct answer, even when i got it a slightly different way, Thanks ! – fearlex Aug 20 '13 at 22:12
1

I got the answer to what i was looking for, but i thank you guys for taking the time to help. Your solution was great but not what i needed. Ok, here is how i got it:

Keeping in mind that $row['event_time'] is a datetime value saved on the database on UTC timezone.

while($row = $result->fetch_assoc())
{
    $s_date = new DateTime($row['event_time'],new DateTimeZone('UTC'));
    $s_date->setTimezone(new DateTimeZone('America/New_York'));
    $start_event_time = $s_date->format('g:i:s A (m/d/y)');
    echo $start_event_time;
}

You can create an additional script that gets the current user timezone and change America/New_York to make it more dynamic.

Hope it helps someone with my same issue.

fearlex
  • 55
  • 1
  • 8