Equivalent of method date.valueOf () JavaScript with PHP - php

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.

Related

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.

difference between two dates in php or javascript or in jquery [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
How to calculate the date difference between 2 dates using php
Hi,,
i am using dd-mm-yyyy format for date;
can u pls give me the code how to find the difference between two dates?
JavaScript
If you're using a string in the format DD-MM-YYYY for your date values, you have two things you need to do: 1. Convert the string into a date, and 2. Get the difference between the dates. I'll take those in reverse order:
Date Difference
In JavaScript, performing arithmetic on Date instances will use their underlying "time" value (date.getTime()), which is the number of milliseconds since The Epoch (and can be negative). So to get the difference, just subtract one date from another:
var d1 = new Date(2010, 0, 1); // January 1st 2010
var d2 = new Date(2010, 1, 1); // February 1st 2010
var diff = d2 - d1; // Milliseconds between the dates (in this case, 2678400000)
Live example
String to Date
If you have your dates in a string in the form DD-MM-YYYY, you'll have to dice up that string to create the Date instances. String#split will split up the string using the delimiter you give it (- in this case) and create an array of strings, and then parseInt will convert those strings into numbers (we specify the radix, in this case 10 for decimal, so that we don't have to worry about parseInt seeing a leading 0 and assuming octal):
function ddmmyyyyToDate(str) {
var parts = str.split("-"); // Gives us ["dd", "mm", "yyyy"]
return new Date(parseInt(parts[2], 10), // Year
parseInt(parts[1], 10) - 1, // Month (starts with 0)
parseInt(parts[0], 10)); // Day of month
}
var s1 = "01-01-2010"; // January 1st 2010
var d1 = ddmmyyyyToDate(s1);
var s2 = "01-02-2010"; // February 1st 2010
var d2 = ddmmyyyyToDate(s2);
var diff = d2 - d1;
Live example
The reason you have to do it yourself is that it's only recently that there was a standard string format you could pass into new Date() to have it parse it into a date instance. A standard (a simplified version of ISO8601) was introduced in the 5th edition spec, but isn't well-supported in the wild yet.
If you use php 5.3, you can use the DateTime object:
http://www.php.net/manual/en/datetime.diff.php
// php
echo strtotime($date1) - strtotime($date2); // echos the time difference between $date1 and $date2 in seconds
In JavaScript, take a look at the Datejs library, which makes working with dates in JavaScript easy. You will need to download the full package which contains globalization modules which affect how dates are parsed and rendered. With the appropriate CultureInfo file:
Date.parse('11-12-2010') - Date.parse('25-12-2010');
This statement returns -1209600000 ms; divide to get other units – /1000/60/60/24 = -14 days. (Dividing by 1000 ms, 60 seconds, 60 minutes, 24 hours to get days.

Getting timezone for JavaScript and PHP all mixed up

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

Categories