Getting timezone for JavaScript and PHP all mixed up - php

I'm trying to synchronize the timezone between a PHP script and some JavaScript code.
I want a PHP function that returns a timestamp in UTC. Does gmmktime() do that?
On the JavaScript side, I have:
var real_date = new Date();
real_date -= real_date.getTimezoneOffset() * 60000;
real_date /= 1000;
Does this convert the timestamp to UTC?

PHP
Just time() will do what you want. If you want an arbitrary timestamp, instead of the current time, then gmmktime will do that, yes.
Returns the current time measured in
the number of seconds since the Unix
Epoch (January 1 1970 00:00:00 GMT).
http://www.php.net/manual/en/function.time.php
Javascript
You can use the .UTC() method of a Date object to get # of milliseconds in UTC. However, your current solution should also work, if you're starting with a timestamp.

You can use time()
For the Javascript question UTC

Related

DateTime->format("U") doesn't return a value with timezone info

In PHP 5.2, I'm using the following code to get a timestamp from a DateTime object.
$dateTime = new DateTime("now", new DateTimeZone("America/Los_Angeles") );
echo $dateTime->format("U");
the problem is that format("U") simply returns server timestamp, which is UTC.
How do I make it to return a timestamp from Pacific Time Zone (Los Angeles) ?
Your concept for timestamp is not right, timestamp is timezone independent, it is defined as the number of seconds that have elapsed since midnight Coordinated Universal Time (UTC), 1 January 1970.
Try setting timezone at top of PHP script. I think timestamps are always UTC. Use date() function to format it into what you need.
// set timezone to pacific time
date_default_timezone_set('America/Los_Angeles');

mktime VS time PHP4 VS PHP5

I download event calender from http://www.phpcodeworks.com/pec/installation. I am using PHP 5.3.X therefore browser said F:\xampp\htdocs\msj\functions.php so I replace :
$days = date("t", mktime(0,0,0,$month,1,$year));
with:
$days = date("t",` time(0,0,0,$month,1,$year));
but doing so each date goes 24 hours back as follows.
Image when using $days = date("t", mktime(0,0,0,$month,1,$year));:
Image when using $days = date("t", time(0,0,0,$month,1,$year));:
The mktime() function returns the time in seconds from Unix Epoch (January 1 1970 00:00:00 GMT) to the date and time provided as parameters.
The time() function retuns the time in seconds from Unix Epoch (January 1 1970 00:00:00 GMT) to the moment the function is run. There are no parameters to pass in.
So when browsing for a particular date, you will need to use mktime() instead of time(), time() will constantly return a different number every time you run it. Because of that, your calendar will change every time you view it (even if you are trying to view a particular date).
Hightlight:
mktime() - Time in seconds representing a specified date (see the documentation for the required parameters).
time() - Time in seconds representing now (there are no parameters for this function).
mktime() looks to be the appropriate function for this situation.

How To Change A Unix Timestamp In The Future To Seconds Remaining

I am trying to create a javascript countdown timer;
I have a string that is in the format of YYYY-MM-DD HH:MM:SS .
This could be any time up to 6 months in the future.
What would be the best way to go about getting the time remaining in seconds from now until the future time. This could be implemented in PHP.
Thanks in advance!
In PHP you can use strtotime, which takes a string representation of a date and returns the unix timestamp.
Then use microtime to get the current unix timestamp, and find the difference. This will be the number of milliseconds remaining, so divide it by 1000 to get it in seconds.
http://php.net/manual/en/function.strtotime.php
http://php.net/manual/en/function.microtime.php
This should work:
$currentTime = explode(" ", microtime());
$currentTime = $currentTime[1];
$futureTime = strtotime("YYYY-MM-DD HH:MM:SS"); // insert your date here
$timeRemaining = ($futureTime - $currentTime) / 1000;
How are you getting this string-based timestamp? A unix timestamp is actually already "number of seconds since 1970-01-01 00:00:00". That looks like a native MySQL date string.
If it is coming out of MySQL, you can convert it to a unix-style timestamp with UNIX_TIMESTAMP(), e.g.
SELECT unix_timestamp(datetimefield) ...
and then convert it to a Javascript timestamp by multiplying by 1000 (JS timestamps have the same epoch, but in milliseconds).
If you're stuck in PHP, you can go quick/dirt with
$timestamp = strtotime($time_string);
$js_timestamp = $timestamp * 1000;

How to pass date from Php date variable to javascript date variable

Hello i need to get this date value from php 1328569380 and convert it to javascript date.
By the way how is this date "1328569380" type of form called ?
The numeric date time your are referring to is called a timestamp. It is the number of seconds elapsed since january 1st of 1970 if i'm not wrong.
To send a date to javascript, just print it out using the timestamp x 1000 since it also accepts millisecond initialization format:
mydate = new Date(<?php echo $mytimestamp*1000; ?>);
Good luck
This is a Unix epoch timestamp. See the following thread for the how-to:
Convert a Unix timestamp to time in JavaScript
Your value is the number of seconds that has passed since 1970-01-01 00:00:00, called the Unix epoch.
JavaScript counts the number of milliseconds instead, thus you have to multiply your timestamp with 1000 prior to using it to create a JavaScript date-object.
var phptimestamp = 1328569380;
var date = new Date(phptimestamp * 1000);

Converting string into Date() in JavaScript

How do I convert this timestamp from php into a javascript Date() object?
This is how I grab the time:
$timestart = time();
and I parse this to a javascript function and I want to convert it into a JavaScript date object.
help, all this date stuff confuses me quite a bit.
thanks,
If val contains your PHP value which is
the current time measured in the number of seconds since the Unix Epoch
then you just need this:
var timestart = new Date(val * 1000);
JavaScript uses the same base time as UNIX systems (midnight on 01/01/1970) but measured in milliseconds rather than seconds.
Solution here :
Convert a Unix timestamp to time in JavaScript
Substring the parts of the timestamp you need to create the Date. Then initialise like so,
var d = new Date(year, month, date);
This is a cross browser implementation.

Categories