Awkward problem -> I'm trying to get current date&time in ISO 8601 format (like this: 2022-02-11 12:30:02.846108).
When I run $time = new Time('now', 'Europe/Amsterdam') in CI4, $time returns this:
object(CodeIgniter\I18n\Time)#79 (6)
{ ["timezone":protected]=> object(DateTimeZone)#80 (2)
{ ["timezone_type"]=> int(3)
["timezone"]=> string(16) "Europe/Amsterdam"
}
["locale":protected]=> string(2) "en"
["toStringFormat":protected]=> string(19) "yyyy-MM-dd HH:mm:ss"
["date"]=> string(26) "2022-02-11 12:30:02.846108"
["timezone_type"]=> int(3)
["timezone"]=> string(16) "Europe/Amsterdam"
}
but when i try to get $time->date returns NULL.
Any ideea how to access date string in order to get the current time with microseconds?
Found out there is no ->date attribute in the DateTime object, which is why $time->date returns NULL.
The proper way to get the date with microseconds is:
$time->format('Y-m-d\TH:i:s.u')
Related
Please let me construct a statement to calculate the number of days between two variables that have the format: 20211024. This is of course the year, then month, then day in a single eight digit variable.
By using the DateTime class?
$d = new DateTime('20211024');
var_dump($d);
$d2 = new DateTime('20211014');
var_dump($d2);
echo $d->diff($d2)->format('%R%a days');
That example code gives the output:
object(DateTime)#7 (3) {
["date"]=>
string(26) "2021-10-24 00:00:00.000000"
["timezone_type"]=>
int(3)
["timezone"]=>
string(3) "UTC"
}
object(DateTime)#8 (3) {
["date"]=>
string(26) "2021-10-14 00:00:00.000000"
["timezone_type"]=>
int(3)
["timezone"]=>
string(3) "UTC"
}
-10 days
Example fiddle
I want to create a DateTime object from a millisecond timestamp but that does not seem to work:
$timestamp_in_ms = 1546300800000; // int or string no difference
var_dump(DateTime::createFromFormat("Uv", $timestamp_in_ms));
var_dump(DateTime::getLastErrors());
This results in:
bool(false)
array(4) {
["warning_count"]=>
int(0)
["warnings"]=>
array(0) {
}
["error_count"]=>
int(1)
["errors"]=>
array(1) {
[13]=>
string(12) "Data missing"
}
}
However if I use a slightly different notation it does work:
$timestamp_in_ms = "1546300800.000";
var_dump(DateTime::createFromFormat("U\.v", $timestamp_in_ms));
var_dump(DateTime::getLastErrors());
object(DateTime)#1 (3) {
["date"]=>
string(26) "2019-01-01 00:00:00.000000"
["timezone_type"]=>
int(1)
["timezone"]=>
string(6) "+00:00"
}
Yes I can do something like this:
$timestamp_with_dot = $timestamp_in_ms / 1000 + "." + $timestamp_in_ms % 1000;
The documentation does not provide any insight in this. Both U and v are supported.
Anybody more info on this issue? I am running PHP 7.4.10
With a division by 1000 you can convert your millisecond timestamp as a float in seconds.
$msTimeStamp = 1600790571478;
$floatSec = $msTimeStamp/1000.0;
When using DateTime::createFromFormat, you can also use the "U.u" format. The "u" format should be exactly 6 characters. This can be done with sprintf().
$dateTime = DateTime::createFromFormat("U\.u", sprintf('%1.6F',$floatSec));
//test
echo $dateTime->format('Y-m-d H:i:s.u');
//2020-09-22 16:02:51.478000
//Timezone "+00:00" (UTC)
The millisecond timestamp can also be a float and also contain microseconds.
The above code works on 32-bit systems too.
Below is the time string I have,
'2019-12-30T15:42:33.891+11:00'
I tried to convert this to date format using below php code.
date('Y-m-d H:i:s',strtotime('2019-12-30T15:42:33.891+11:00'));
And i am getting 2019-12-30 10:12:33 as the output. It seems date getting correctly but time not.
How to display the time also correctly? Thanks in advance!
This date and time format relates to the "Zulu time" (UTC). UTC is refereed as Universal Time Coordinated. It is also known as “Z time” or “Zulu Time”.
You should change your timezone by some command like below code:
$date = new DateTime('2019-12-30T15:42:33.891+11:00', new DateTimeZone('UTC'));
echo $date->format('Y-m-d H:i:s');
Or, for having more tools you can use the Carbon library.
Your time string '2019-12-30T15:42:33.891+11:00' contains time zone information of +11:00.
With date() the date and time are converted into the local time zone of your server. For example, I get "2019-12-30 05:42:33" (Timezone Europe/Berlin).
You can set a different time zone with date_default_timezone_set() or use DateTime to output the time for a location with the time zone "+11:00".
echo date_create('2019-12-30T15:42:33.891+11:00')->format('Y-m-d H:i:s');
//2019-12-30 15:42:33
strtotime () returns an integer timestamp. The Iformation of the Timezone +11:00 are lost in the process, but are processed when the time stamp is determined.
The DateTime object always has a time zone.
echo '<pre>';
var_dump(date_create('2019-12-30T15:42:33.891+11:00'));
Output:
object(DateTime)#1 (3) {
["date"]=>
string(26) "2019-12-30 15:42:33.891000"
["timezone_type"]=>
int(1)
["timezone"]=>
string(6) "+11:00"
}
Note (Update):
When creating a DateTime object, the optional parameter for the time zone is only taken into account if the time string does not contain a time zone!
var_dump(new DateTime('2019-12-30T15:42:33.891 +11:00', new DateTimeZone('UTC')));
object(DateTime)#1 (3) {
["date"]=>
string(26) "2019-12-30 15:42:33.891000"
["timezone_type"]=>
int(1)
["timezone"]=>
string(6) "+11:00"
}
//without +11:00
var_dump(new DateTime('2019-12-30T15:42:33.891', new DateTimeZone('UTC')));
object(DateTime)#1 (3) {
["date"]=>
string(26) "2019-12-30 15:42:33.891000"
["timezone_type"]=>
int(3)
["timezone"]=>
string(3) "UTC"
}
I'm using the following format Y-m-d\TH:i:s.v\Z to follow the JavaScript toISOString implementation (2011-10-05T14:48:00.000Z).
Everything works fine if I have a DateTime and I want to format it, however I cannot parse a string that uses this format.
$format = 'Y-m-d\TH:i:s.v\Z';
$stringDateTime = (new \DateTime())->format($format);
var_dump(date_create_from_format($format,$stringDateTime));
I'm using PHP 7 and I have tested the code above with PHP 7.0,7.1 and 7.2. The return that I expect in line 3 is a DateTime class, however I'm getting a false due to there is a parse problem.
I hope someone can clarify this behavior.
Thanks
Datetime will handle it fine, you won't need to create from format.
<?php
$format = 'Y-m-d\TH:i:s.v\Z';
$stringDateTime = (new \DateTime())->format($format);
var_dump(date_create($stringDateTime));
https://3v4l.org/phLE1
Result:
object(DateTime)#1 (3) {
["date"]=>
string(26) "2018-03-13 18:07:30.005000"
["timezone_type"]=>
int(2)
["timezone"]=>
string(1) "Z"
}
This also works:
<?php
$format = 'Y-m-d\TH:i:s.u\Z';
$stringDateTime = (new \DateTime())->format($format);
var_dump(date_create_from_format($format, $stringDateTime));
$format = \DateTime::ISO8601;
$stringDateTime = (new \DateTime())->format($format);
var_dump(date_create_from_format($format, $stringDateTime));
https://3v4l.org/FcUqe
object(DateTime)#1 (3) {
["date"]=>
string(26) "2018-03-13 18:15:10.011717"
["timezone_type"]=>
int(3)
["timezone"]=>
string(16) "Europe/Amsterdam"
}
object(DateTime)#1 (3) {
["date"]=>
string(26) "2018-03-13 18:15:10.000000"
["timezone_type"]=>
int(1)
["timezone"]=>
string(6) "+01:00"
}
I've got a variable that upon var_dump prints this.
It contains a date, but I just don't know how to access the date data, if I put
$myVariable[date] OR $myVariable["date"] OR $myVariable['date'] all result in error.
object(DateTime)#8 (3) {
["date"]=> string(19) "2011-07-25 00:00:00"
["timezone_type"]=> int(3)
["timezone"]=> string(16) "Europe/Stockholm"
}
I don't understand what the first number is either, the #8 right after the (DateTime)...
echo $myVariable->format('Y-m-d H:i:s');
(thanks to kmfk for that answer)