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);
Related
$dayBasedOnUTC = date('l', $_GET['day']);
Why is it that when I echo the value of $dayBasedOnUTC the day returned is a Tuesday?
The UTC value of $_GET['day'] is: 1409393126144
If you put that number into any Unix Timestamp Converter you will see the date is Saturday.
It appears that 1409393126144 is a Javascript timestamp, which is counted in milliseconds. PHP expects its UNIX timestamps in seconds though. So 1409393126144 to PHP is a timestamp in the far future.
Divide by 1000 to get the correct value:
echo date('l', 1409393126144 / 1000);
I have a php variable say $expTime (which has a unixtime say-1359683953) . I want to display this variable on the client side(in a proper time format according to his local time) . I am so confused between the UTC ,GMT , DST all that things. Can anyone suggest a solution for this using php or javascript please.
when I am using echo date('h:i M d/y',$expTime) it is showing me a wrong time.
How I am saving the time to database:
var exp_day= parseInt($("#exp_day").val());
var exp_hrs= parseInt($("#exp_hrs").val());
var exp_min= parseInt($("#exp_min").val());
var exp_time = (exp_day*24*60*60) + (exp_hrs*60*60) + (exp_min*60) ;
then I posted the exp_time using ajax to a php file -
$expTime = time() + $_POST["exp_time"];
What I am retrieving from the database is $expTime . This $expTime I want to display it on the all the clients system according to there local time zone (also by making sure the day light saving)
Use DateTime with timezones:
$datetime = new DateTime('#1359683953');
echo $datetime->format('h:i M d/y') . "<br>";
$datetime->setTimezone(new DateTimeZone('America/Los_Angeles'));
echo $datetime->format('h:i M d/y');
See it in action
UNIX time values are usually UTC seconds since epoch. Javascript time values are UTC milliseconds since the same epoch. The ECMA-262 Date.prototype.toString method automatically generates a string representing the local time and takes account of daylight saving if it applies.
You can also use Date methods to create your own formatted string, they also return local time and date values. There are also UTC methods if you want to work in UTC.
To do this on the client, just provide a UTC time value from the server and then all you need in javascript is:
var timeValue = 1359683953; // assume seconds
var date = new Date(timeValue * 1000); // convert time value to milliseconds
alert(date); // Fri 01 Feb 2013 11:59:13 GMT+1000
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 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.
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