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

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;

Related

converting unixtime to mysql datetime retaining fractions of a second

$id["REQUEST_TIME_FLOAT"]="'".date( 'Y-m-d H:i:s.', $_SERVER["REQUEST_TIME_FLOAT"]).substr((($_SERVER["REQUEST_TIME_FLOAT"]-floor($_SERVER["REQUEST_TIME_FLOAT"]))),2,20)."'";
Say $_SERVER["REQUEST_TIME_FLOAT"] =157888888888888.98765
$t=new DateTime()
$t->setTimestamp( only accept an integer) Fail
Also the date command just cuts it off, even if you add u to the format it just adds 00000 and not the real number of milliseconds.
Most functions I can find just cut off the .98765 and I don't want that.
I put together a hack shown above. Is there a better way and/or more cpu efficient way of doing this.
Try using DateTime::createFromFormat("U.u", $_SERVER["REQUEST_TIME_FLOAT"] );
The docs say that date() will trim the milliseconds because it expects an integer, but this function won't.

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.

Using PHP to Match a Javascript Timestamp

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.

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.

PHP strtotime without leap-years

With regards to this thread, I've developed a partial solution:
function strtosecs($time,$now=null){
static $LEAPDIFF=86400;
$time=strtotime($time,$now);
return $time-(round((date('Y',$time)-1968)/4)*$LEAPDIFF);
}
The function is supposed to get the number of seconds given a string without checking leap-years.
It does this calculating the number of leap-years 1970 [(year-1986)/4], multiplying it by the difference in seconds between a leap-year and a normal year (which in the end, it's just the number of seconds in a day).
Finally, I simply remove all those excess leap-year seconds from the calculated time. Here's some examples of the inputs/outputs:
// test code
echo strtosecs('+20 years',0).'=>'.(strtosecs('+20 years',0)/31536000);
echo strtosecs('+1 years',0).'=>'.(strtosecs('+1 years',0)/31536000);
// test output
630676800 => 19.998630136986
31471200 => 0.99794520547945
You will probably ask why am I doing a division on the output? It's to test it out; 31536000 is the number of seconds in a year, so that 19.99... should be 20 and 0.99... should be a 1.
Sure, I could round it all and get "correct" answer, but I'm worried about the inaccuracies.
Edit1: Since it doesn't seem obvious, my problem is with inveteracies; you just don't ask PHP for 20 years and it gives you 19.99..., right?
Edit2: It all seems to boil down to the part about 1968;
1970; found it accurate in all tests I've tried.
1969; Found it used here (...ex: (2008-1969)/4 = 9.75...) as well as mentioned here. Accurate after the 2nd year (+3 years) onwards.
1968; as detailed below, this is "year zero" of leap years from unix time (1970). It sounds "right" (to me) but it isn't accurate, at all.
Could this be related to the inherent inaccuracy experienced when using PHP to manage floating point numbers?
http://php.net/manual/en/language.types.float.php
You should replace 1968 in your calculation (where does it come from ?) by the origine of unix time : 1970 and you will get more accurate results.
Edit
You have to do an intval to count the number of leapyears which must be an integer :
return $time - (intval( (date('Y', $time) - 1969) / 4) * $LEAPDIFF);
This will give you correct results within the range +0 -> +68 , end of unix time on 32bit machine

Categories