converting unixtime to mysql datetime retaining fractions of a second - php

$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.

Related

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

Ignore BST with php strtotime() function

Is there a way for me to tell the strtotime() function not to change the time I give it into BST? i.e. if do
date('g.ia', strtotime("2014-06-25T19:30"))
I want to get 7:30pm, just as if I entered
date('g.ia', strtotime("2014-06-25T19:30"))
(The first one currently returns 6:30pm)
I'm aware I could just write a manual check for the day/month and add an hour if necessary, or just parse the time myself from the string, but both solutions sound a bit messy (I'll have to do this in quite a few places).
Sorry if there's something obvious I'm missing, pretty new to php
Function date() will format time based on your timezone setting. Said that, your example doesn't make sense since strtotime() will use current timezone setting to convert input to unix timestamp, and then function date() will use again that timezone setting to format timestamp back. You must be changing timezone setting between strtotime() and date() function calls, like this demo.
You can simply use DateTime extension, where you implicitly tell in what timezone is your time:
$dt = new DateTime('2014-06-25T19:30', new DateTimezone('Europe/London'));
echo $dt->format('g.ia');

epoch date conversion

I'm trying to convert an epoch timestamp with php 5.3 with the following statement
date('d-m-Y',strtotime('1349042399999'));
to human readable format and getting wrong result: 01-01-1970what should return30-09-2012. I have been searching around and founding the following topic PHP strtotime returns a 1970 date when date column is null but did not help on my case.
The reason for that is that there are milliseconds embedded in that timestamp, which causes it to go over the integer overflow limit.
chop the last 3 characters, and you're good to go:
$original_timestamp = "1349042399999";
$timestamp = (int) substr($original_timestamp,0,-3);
echo date('d-m-Y',$timestamp);
By using strtotime, you are trying to convert a timestamp into another timestamp. Your timestamp is also in microseconds. The date function just needs the timestamp without the microseconds like so:
date('d-m-Y',substr('1349042399999', 0, -3));
I believe that the Second Rikudo solution worked because of the cast to int, not because of trimming the milliseconds.
It seems to me that this should work for you (it did for me)
$original_timestamp = "1349042399999";
date('d-m-Y', (int) $original_timestamp);

Converting SQL datetime into separate variables

I've been working on a blog commenting system and have been inserting the datetime with NOW(), and I can't quite manage to pull it back into manageable variables,
I've tried using strtotime like following
$date_time1 = strtotime( $row['chron']);
echo $date_time1;
also with mktime
$timeywimey = mktime ( $row['chron'] );
both of these are turning 2011-03-24 12:01:59 into 1300964519
Iif possible I would be looking to split these down into yy,mm,dd hh,mm
Although it's just basic string manipulations should be known to every PHP user, you shouldn't split this value but rather format it in desired format, by using either SQL date_format() function or PHP date() one. For the latter it would be
$date = date("format",strtotime($row['chron']));
for sake of splitting practice, you have either to use some regular expression(harder) or couple calls of explode(), to split value into date ant time parts and then consequently split these into smaller parts.

formatting dates with or without a leading zero in php

How would you convert a datestring that will be formated like...
m/d/yyyy H:i:s or mm/dd/yyyy H:i:s...
and format it like... yyyy-mm-dd H:i:s
I can format either of the two inputs into my desired format, but not both.
function format_date($date){
return date('Y-m-d H:i:s',strtotime($date));
}
should do the trick.
Easiest way would be to simply transform it to unix time first with strtotime then run strftime on it afterwards... not the bes practice but it eliminates alot of the potential format parsing issues :-)
How are you formatting it now? If you're certain that those are the only two date formats you'll possibly have as input, then you can explode() it on the forward slashes, then if strlen() returns 1 on the month or the date, add a 0 as you're creating your yyyy-mm-dd string.
However, if you're not guaranteed that those are your only two input formats, then I would use strtotime() to convert it to epoch, then use date() to format it as you desire. A slightly bigger processing hit, but much more universal code.

Categories