I'm trying to parse Atom feed and it contains date like this one
2019-02-28T06:11:12.0775241Z
The problem with it is microseconds 0775241, which is 7 digits, but php can handle only 6 digits. This date is probably generated with Go lang.
If it had 6 digits, Y-m-d\TH:i:s.u\Z would work fine.
UPD: it is for DateTime::createFromFormat, can't use new DateTime etc.
Looking for solution, may be you had same problem as me.
That is a standard easily recognisable date format. Use the DateTime class
$d = new DateTime("2019-02-28T06:11:12.0775241Z");
echo $d->format('d/m/y H:i:s');
RESULT
28/02/19 06:11:12
Of course you can output in any format you like
I know this is not the best solution but you can handle it with Carbon
$datetime = Carbon::parse('2019-03-13T18:07:54.5810814+03:30');
dump($datetime);
// Carbon #1552487874 {#358
// date: 2019-03-13 18:07:54.581081 +03:30
// }
Related
I'm using format W-Y for weeknumber & year.
e.g. the final week of 2018 would be represented as '52-2018'.
But I can't get Carbon or DateTime to convert it back.
>>> Carbon::createFromFormat('W-Y', '01-2018')
InvalidArgumentException with message 'The format separator does not match
The separation symbol could not be found
Trailing data'
DateTime::createFromFormat (which is what Carbon extends) doesn't support the W formatting character, unfortunately.
The easiest way to work around this is to create a new DateTime (or Carbon) instance, and use the native setISODate method to set the year and week number:
$str = '01-2018';
list ($week, $year) = explode('-', $str);
$d = new DateTime;
$d->setISODate($year, $week);
See https://3v4l.org/g33QV
A string of the form '01-2018' can also be converted to '2018W01' with preg_replace, which can then be processed directly by DateTime and Carbon.
$str = '01-2018';
$dateTime = new DateTime(preg_replace('~^(\d\d)-(\d\d\d\d)$~','$2W$1',$str));
While with the accepted solution we always get the current time for the date, here it is always 00:00:00.
Demo: https://3v4l.org/mo6dQ
Ok, so I've got a time string like this:
2013-08-09T15:00:00
Now as far as I can tell, I'll need to convert this to a timestamp, before I can change the formatting - is that correct, or is there a shorter step? For example using one of the classes here:
http://philsturgeon.co.uk/blog/2012/08/why-php-datetime-rocks
I'm not quite sure what the "T" represents (besides time, obviously) and I'm not sure what format that is.
I want to get it into standard 12 hour time.
Use DateTime()
$dt = new DateTime('2013-08-09T15:00:00');
echo $dt->format('Y-m-d h:i:s');
I'm facing an issue with managinging dates, some dates pass others dont. I want to produce an insertable date for mysql. there are two possible types of post dates
yyyy-mm-dd //should go without conversion
m/d/yyyy // should be converted
I'm using this
$date = $_REQUEST['date'];
$date_regex = '/(0[1-9]|1[012])[- \/.](0[1-9]|[12][0-9]|3[01])[- \/.](19|20)\d\d/';
if(preg_match($date_regex, $date)){
$date = DateTime::createFromFormat('m/d/Y',$date)->format('Y-m-d');}
problems
I realised this regex is failing for dates like
2/5/2013
but has been working for
12/12/2013
so I removed it BUT still
DateTime::createFromFormat('m/d/Y',$date)->format('Y-m-d');
is also failing for m/d/yyyy
This date thing has got my head spinning for the last 6 hours.
In this case, there is no need to use DateTime::createFromFormat because m/d/yyyy is one of the recognized date formats (see "American month, day and year"). Just convert it to a DateTime object and let the constructor handle the format and forget the regex:
$date = $_REQUEST['date'];
$datetime = new DateTime($date);
$datex = $datetime->format('Y-m-d');
The reason DateTime::createFromFormat('m/d/Y',$date) fails for dates like 2/5/2013 is because you are forcing it to be specifically 'm/d/Y' and that date does not fit that pattern. You can see a list of all date formats here. Specifically, m expects there to be a leading zero (like 02), so when you give it one without that, it won't recognize it. Same goes for d. In this case you would have to use n and j respectively. But, like I said, let the constructor do the hard work for you.
I need to display and handle UTC dates in the following format:
2013-06-28T22:15:00Z
As this format is part of the ISO8601 standard I have no trouble creating DateTime objects from strings like the one above. However I can't find a clean way (meaning no string manipulations like substr and replace, etc.) to present my DateTime object in the desired format. I tried to tweak the server and php datetime settings, with little success. I always get:
$date->format(DateTime::ISO8601); // gives 2013-06-28T22:15:00+00:00
Is there any date format or configuration setting that will give me the desired string? Or I'll have to append the 'Z' manually to a custom time format?
No, there is no special constant for the desired format. I would use:
$date->format('Y-m-d\TH:i:s\Z');
But you will have to make sure that the times you are using are really UTC to avoid interpretation errors in your application.
If you are using Carbon then the method is:
echo $dt->toIso8601ZuluString();
// 2019-02-01T03:45:27Z
In PHP 8 the format character p was added:
$timestamp = new DateTimeImmutable('2013-06-28T22:15:00Z');
echo $timestamp->format('Y-m-d\TH:i:sp');
// 2013-06-28T22:15:00Z
In order to get the UTC date in the desired format, you can use something like this:
gmdate('Y-m-d\TH:i:s\Z', $date->format('U'));
To do this with the object-oriented style date object you need to first set the timezone to UTC, and then output the date:
function dateTo8601Zulu(\DateTimeInterface $date):string {
return (clone $date)
->setTimezone(new \DateTimeZone('UTC'))
->format('Y-m-d\TH:i:s\Z');
}
Edit: clone object before changing timezone.
Since PHP 7.2 DateTimeInterface::ATOM was introduced in favor of DateTimeInterface::ISO8601, although it still lives on for backward compatability reasons.
Usage
$dateTimeObject->format(DateTimeInterface::ATOM)
how to compare this date format in PHP ? 30-APR-12 03.46.59.000000000 PM
You could create a DateTime object from that format and then get the date in any format you want - http://us2.php.net/manual/en/datetime.createfromformat.php
you could use the DateTime function createfromformat() (here) . If you make a timestamp out of that (using strtotime or getTimeStamp) it is easy to compare to other timestamps.
I am not sure what the last 9 digits are for. If it's milli seconds php supports just up to 6 digits with u thats why I'd used * to drop the last digits.
$date = DateTime::createFromFormat('j-M-y H.i.s.u*', '30-APR-12 03.46.59.000000000 PM');
Further information can be found here: http://php.net/manual/en/datetime.createfromformat.php