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.
Related
I have this script in JavaScript:
var dt = new Date();
var intDt = dt.valueOf();
console.log(intDt); // 1504100049524
I want to covert this to PHP but in my surprise the result is not the same.
This is my PHP script:
$dt = date(DATE_RFC2822);
$intDt = strtotime($dt);
echo($intDt); //1504100049
I need this to calculate the moon phase, fraction and angle.
The calculus of the phase, fraction and angle of the moon works fine in my js script. But the result of my PHP is different of the JavaScript because of this.
PHP's getTimestamp() will give you the UNIX timestamp, i.e. number of seconds since January 1st, 1970.
More info here: Unix Time
Javascript valueOf() will give you the number of milliseconds since the same date.
More here: Date.prototype.valueOf()
With those in mind here's a snippet in javascript:
var jsdt = new Date('2018-08-18')
undefined
jsdt.valueOf()
1534550400000
And here's how you would do the "same" in PHP:
$external = "08/18/2018 00:00:00";
$format = "m/d/Y H:i:s";
$dateobj = DateTime::createFromFormat($format, $external);
echo $dateobj->getTimestamp()*1000; // 1534550400000
PHP's time functions return the time in seconds since the unix epoch not milliseconds like JS. You either need to divide the JS time by 1000 and discard the remainder or multiply the PHP time by 1000. microtime with the second parameter set to true could be used but it only retrieves the current time.
I am trying to connect to the server every minute and pass in the variable of the current time, which would be the lastUpdate. On the server side php file, I plan to compare the passed in time variable with a TIMESTAMP in the database rows; much like if (time1-time2 > certain value)... So far the javascript code is:
var time;
var timer_is_on = 0;
localStorage.lastUpdate = 0;
localStorage.numUpdates = 0;
function timedCount()
{
localStorage.lastUpdate = new Date();
//connect to server
contactServer(localStorage.lastUpdate);
currentCount=currentCount+1;
time=setTimeout("timedCount()",60000);
}
My question is whether I am doing this correctly. I am declaring localStorage.lastUpdate as new Date() and I'm not sure whether this is correct? I have tried the loop and every minute lastUpdate seems to be the same date and time.
My last question is whether I can actually compare the two time formats from javascript and php. In the SQL timestamp, the format is 2012-03-20 11:14:40 while the date format from the javascript new Date() is Tue Mar 20 2012 12:32:44 GMT-0400 (Eastern Daylight Time).
Any information would be helpful, thanks!
Be careful when you use time. Your server time may be different from your client time. The best for you is to store the date of the last request in $_SESSION['last'].
With this solution, all your problems are not anymore. Your PHP set the date, so it is the same reference, and it's in a good format.
Just keep your timeout on the client side:-)
Use new Date().getTime() to return a UNIX timestamp; that is, the number of seconds since Jan 1st, 1970. This makes it much easier to compare with other times.
localStorage.lastUpdate = new Date().getTime();
Will return something like this: 1332266002.
The PHP equivalent is simply time().
And to convert that UNIX timestmap into a MySQL TIMESTAMP, use MySQL's FROM_UNIXTIME function.
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;
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);
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