Using PHP to Match a Javascript Timestamp - php

Alright when I do the code:
<script type = "text/javascript" >
document.write((new Date).getTime());
</script>
You get the Unix timestamp in miliseconds. I need something that will match this in PHP. I need a variable to be set to that, NOT necessarily printed. The variable needs to be a string OR number without any formatting (decimals, commas, etc).
The numbers don't have to match exactly, but have to be close. I tried doing time()*1000 but it turns it into scientific notation and I couldn't format it out without messing up the string.
Thanks so much for any help

If you don't need (millisecond) presision, then just divide & Math.floor the javascript's function. So:
time();
and
Math.floor((new Date).getTime()/1000)
should return the same value at the same time.

What you are looking for is millisecond time in PHP. To accomplish this you need to use a combination of the microtime function and some multiplication.
microtime when passed true as its first parameter will return the current time as the number of seconds since the Unix epoch to the nearest microsecond.
To convert the value into an integer value of the number of milliseconds since the Unix epoch you must multiply this value by 1000 and cast to an integer.
So:
$milliseconds = (int)(microtime(true) * 1000);

The javascript
getTime() ;
method returns the number of milliseconds since midnight of January 1, 1970 and the specified date.
A php equivalent is
time() * 1000; // not microtime() as I wrongly said earlier.
However they wont match as php does not support millisecond precision it seems.

Related

What is the Dart/Flutter equivalent of PHP's gmmktime()?

I need to calculate the value in Dart/Flutter, as calculated by the gmmktime() function in PHP.
This is what I was trying till now:
var ms = (DateTime.now().toUtc().millisecondsSinceEpoch)/100;
int ms = DateTime.now().toUtc().millisecondsSinceEpoch;
But both of these approaches give a value, which is not expected by this API in its header, here: https://api.kitewalk.com/#authentication
PHP's gmmktime is documented to return "Unix time", which is the number of seconds since the the "Unix epoch".
Your first approach was almost right, but you didn't convert from milliseconds to seconds correctly. There are 1000 milliseconds in a second, so you need to divide by 1000, not 100. Additionally, whatever you're passing the time to probably expects an integral number of seconds and not a floating point value, so you'll need to use integer division or round the quotient afterward.
Also note that the "Unix epoch" is not time-zone dependent; DateTime.millisecondsSinceEpoch already measures against a fixed point in time, so an explicit conversion to UTC isn't necessary (but it doesn't hurt).
A correct version would be:
var unixTime = DateTime.now().millisecondsSinceEpoch ~/ 1000;

PHP Date and Time Format for an API request to Linnworks

IN the Linnworks API documentation and Start and End Date is required for one of the API requests. The format for this is as follows;
2018-02-19T16:57:07.0049771+00:00
I am unsure on this formatting. Is this a default formatting of some sort or would I need to construct it?
If I need to construct, I get the obvious portions;
Date;
2018-02-19
Time;
T16:57:07
But what this portion is I do not know;
0049771+00:00
Is it the Unix Time Stamp and a + for time zone?
The end part is microseconds and timezone.
If you use date("c") or $yourDateObject->format("c") it should give you a complete string in this format (ISO 8601).
This is ISO8601 format, which states:
Decimal fractions may be added to any of the three time elements.
However, a fraction may only be added to the lowest order time element
in the representation.
Considering the use of the word "may" here, I would expect that the API should allow you to specify the timestamp without any such decimal portion, assuming that is acceptable for your application. (Disclaimer -- this is a guess.)
If so, this can be had simply via:
echo date('c');
Which yields:
2018-03-20T16:24:37-04:00
Format character u(microseconds) will return 6 char long string (on php<7.1 it will be 000000).
If you need 7 char long string, just prepend 0 to the end, like this:
$dt = new DateTime();
echo $dt->format('Y-m-d\TH:i:s.u0P');
It will output something like this: 2018-03-21T08:47:01.0263140+01:00

How to match string values of datetime format in both Jquery and PHP?

For example -
In Jquery using the function (new Date()).getTime() am getting current datetime as 1470291303352.
But In PHP using strtotime(date('H:i:s')) am getting it as 1470291299.
Here i need to get the same string values. How to do it?
Firstly, php returns the number of seconds since 1970/01/01, jquery returns a number of milliseconds, so there is no way to be the same value.
Second - even if you've got the fastest server in the world it comes to the milliseconds in the execution of lines of code. So exactly the same value can hardly be achieved :)
What you can do to try to trim jquery for the last three numbers representing the milliseconds (this of course if you do these two lines of code to execute in one second :))
And for last, there is a issue of clocks on your server and client computer - it must be exactly the same.
The javascript method getTime() returns microseconds (http://www.w3schools.com/jsref/jsref_gettime.asp) whereas PHP time() (or in your case strtotime() http://php.net/manual/en/function.strtotime.php) returns seconds. The first depends on your clients clock, the latter on your servers clock...
Mostly you never will get the same timestamps this way... maybe you could work around using some kind of AJAX api to have the same timestamp on both sides...
Check this :
In php:
echo strtotime(date('H:i:s')); // 1470294647
In Script :
var date = new Date();
var d = Date.parse("'"+date+"'")/1000; // 1470294647
alert(d);

Get current timestamp with milliseconds [duplicate]

This question already has answers here:
How to get current time in milliseconds in PHP?
(16 answers)
Closed 7 years ago.
I want to get current timestamp with milliseconds in PHP something like below, in JavaScript I use Date.now()
1436030635348
I tried something like below:
$time = str_replace(".","",microtime(true));
But sometime it is not working properly. Like it prints one or two digits less.
It can print less digits, because it is a float value. So if you get 1234.0005, that `.0005 actually means 500 microseconds. The zeroes after the 5 are lost because it's still an unformatted float value.
The function microtime is based on the system call gettimeofday(), which also isn't accurate to the microsecond. Commonly it's accurate to 10 microseconds. See also Linux - Is gettimeofday() guaranteed to be of microsecond resolution.
-edit- I see the specs in your question have changed from microseconds to milliseconds.
To get the float value you have as an integer, you can multiply by a value. The float value represents seconds. Multiply by 1000 to get milliseconds or 1,000,000 to get microseconds. Since the specs (now) say milliseconds, you should multiply by 1000. 10k will give you accuracy of 1/10ms = 100μs. A millisecond is one thousandth of a second. A microsecond is one millionth of a seconds.
Long story short, to get the time in integer milliseconds, use this:
$milliseconds = intval(microtime(true) * 1000);
Note: there is a reason why you get the time as a string or a float by default. The reason is that on 32 bit systems, PHP's integer is also 32 bits and is not large enough to contain the timestamp including milliseconds and microseconds. So this solution will only work well on a 64 bit system.
$timeStampData = microtime();
list($msec, $sec) = explode(' ', $timeStampData);
$msec = round($msec * 1000);
$result = $sec . $msec;
Something like this
But note, that js date.now() return time in MILLIseconds, not MICROseconds
I think you just need to use this function to get a Timestamp with micro seconds in PHP:
int microtime ( void )
Use it as shown in the following link, following code snippet shows you how I'd altered it according to be helpful to your question:
<?php
function microtime_microseconds()
{
list($usec, $sec) = explode(" ", microtime());
return round(($usec * 1000) + $sec);
}
$micro_time = microtime_microseconds();
echo $micro_time;
?>
And also for reference regarding to that microtime() PHP function, please visit and read this PHP manual page.

Microseconds unix time integer as a string in PHP

I need the current unixtime as a round number of microseconds in a string. There's a chance the code could run on a machine with 32 bit integers so I'd beter avoid ints. The value is used only in SQL queries, so a string will be fine.
Is it safe to use the following code?
$x = explode(' ', microtime());
$y = $x[1] . substr($x[0], 2, 6);
Is it safe to assume the coordinates of these substrings in microtime()'s return value are invariant?
Is it safe to assume the coordinates of these substrings in microtime()'s return value are invariant?
Yes it is, pretty much. The string return value is there since longer time an preserved. One proposed change is to make the optional argument defaulting true, however, then you can set it to false to get the string.
It's documented as such in the PHP manual, so as long as you trust that PHP won't starting breaking backward compatibility any time soon, then yes, you can trust this.
By default, microtime() returns a string in the form "msec sec", where sec is the current time measured in the number of seconds since the Unix epoch (0:00:00 January 1, 1970 GMT), and msec is the number of microseconds that have elapsed since sec expressed in seconds.
If get_as_float is set to TRUE, then microtime() returns a float, which represents the current time in seconds since the Unix epoch accurate to the nearest microsecond.
Edit nevermind, I misunderstood.

Categories