Parse date with 2-digit year - php

I want to parse a string of the form 02.01.19 13:49 with Carbon.
Now \Carbon\Carbon::parse('02.01.19 13:49'); fails with
DateTime::__construct(): Failed to parse time string (20.01.19 13:49)
at position 9 (1): Double time specification
Is there any way to tell Carbon that the 19 represents 2019?
This is how I managed to do it, buts its a bit ugly:
\Carbon\Carbon::parse(
preg_replace('/(\d\d\.\d\d\.)(\d\d) /','${1}20$2 ', '02.01.19 13:49')
);

You can use the method Carbon::createFromFormat() to create a carbon instance from a custom date format string.
Carbon\Carbon::createFromFormat('m.d.y H:i', '02.01.19 13:49');
You can reference the PHP date format string from http://php.net/manual/en/function.date.php

Carbon::createFromFormat("d.m.y H:i", "02.01.19 13:49");
This could solve the issue if the input format is fixed.

Related

how to compare two dates in if condation in laravel blade?

in my controller i used Carbon to get current timestamp like showing below:
$current_timestamp = Carbon::now()->format('j/n/Y');
the output of the above:
18/8/2022
and i am getting data from external API like showing below (from blade):
$data[0]['DocDate']
the output of the above:
18/8/2022 12:00:00 AM
now i want to remove 12:00:00 AM from it
i tried in blade view to do:
{{Carbon\Carbon::parse($data[29]['DocDate'])->toDateString()}}
but i am getting this error:
Could not parse '18/8/2022 12:00:00 AM': Failed to parse time string (18/8/2022 12:00:00 AM) at position 0 (1): Unexpected character
and i tried:
$data[29]['DocDate']->format('j/n/Y')
and i get this error:
Call to a member function format() on string
how can i overcome this issue?
You can use create from format function to change the format of incoming date as below:
$inDate = $data[0]['DocDate'];
$outDate = Carbon::createFromFormat('d/m/Y h:i:s a', $inDate )->format('d/m/Y');
On the principle of Keep It Simple - Why not just compare them as strings?, the longer one can easily be shortened, then just compare.
Accepted this is not a "purest" approach, but the format of a "standard" date is not likely to change.
now i want to remove 12:00:00 AM from it
So you don't actually care about date, you don't need Carbon or parsing according to format, you just need to keep the first word of your string:
{{ explode(' ', $data[0]['DocDate'])[0] }}

Carbon PHP parse datetime with dot notation

I'm using Carbon 2 and am having an issue with some strange dates in my application that are given to me by a third party api, ideally they'd always be in the YYYY-MM-DD format but a few are coming in the following format: YYYY.MM.DD.
How can I use Carbon::parse() or PHP to reliably convert the string to hyphens and then parse it if in that format?
Example:
echo Carbon::parse('2022.07.24 10:34:05');
This is invalid.
Maybe I just look for the dots and change to hyphen and try re-parsing?
As described in the documentation:
$date = Carbon::createFromFormat('Y.m.d H:i:s', '2022.07.24 10:34:05');
In your case, you can normalize the string before the parse():
$str = '2022.07.24 10:34:05'; // or '2022-07-24 10:34:05'
echo Carbon::parse(strtr($str, ['.' => '-']));

How to parse date string to dateTime in php?

I have a date string in this format "2014-12-01 16:00:02,153". How do I parse this to a datetime format for mysql?
I am using laravel and Carbon. My code is Carbon::parse("2014-12-01 16:00:02,153");
but get an error that reads DateTime::__construct(): Failed to parse time string (2014-12-01 16:00:02,153) at position 20 (1): Unexpected character
You could use the createFromFormat method from the DateTime object :
$dateTime = DateTime::createFromFormat('Y-m-d H:i:s,u', "2014-12-01 16:00:02,153");
echo $dateTime->format('Y-m-d H:i:s');
PHP is confused by the time format you are handing over.
new DateTime("2014-12-01 16:00:02,153"); // fails
strtotime("2014-12-01 16:00:02,153"); // fails
This is because of the unexpected part with the comma at the end. Where are you getting this strange timestamp with comma seperated milliseconds from? It is unusual.
As you are asking for datetime which usually does not need any milliseconds you can do the following:
$timeparts = explode(",","2014-12-01 16:00:02,153");
Carbon::parse($timeparts[0]); // should work
$timeparts[1] is the milliseconds then.
If you want to be more precise you can round the milliseconds:
if(intval($timeparts[1]) >= 500) {
// time +1 second
}
... or you treat the milliseconds seperatly if you need em. Add them to the generated timestamp later and check the docs of Carbon.

PHP convert string into date time

I have a php string from db it is 20/11/2017 I want to convert it milliseconds.
It's my code to doing that.
$the_date = "20/11/2017";
$mill_sec_date = strtotime($the_date);
var_dump($mill_sec_date);
But it does not print any thing rather than
bool(false);
What is the problem and how can i solve it ????
When using slashes to separate parts of the date, PHP recognizes the format as MM/DD/YYYY. Which makes your date invalid because there is no 20th month. If you want to use the format where day and month is swapped, you need to use hyphens, like DD-MM-YYYY.
$time = strtotime('10/16/2003');
$newformat = date('Y-m-d',$time);
print_r($newformat);
Use DateTime class to call function createFromFormat
$date = date_create_from_format('d/M/Y:H:i:s', $string);
$date->getTimestamp();
Most likely you got the date format wrong, see
here for a list of supported date and time formats:
This section describes all the different formats that the strtotime(), DateTime and date_create() parser understands.
You string is not accept by the strtotime, you can use createFromFormat set set the with the format type of the time string like below, you can also check the live demo. And you also can refer to this answer
var_dump(DateTime::createFromFormat('d/m/Y', "20/11/2017"));

Carbon/DateTime: The timezone could not be found in the database

I'm reading a CSV file with datetime fields but when I try to convert the date to Carbon by doing $date = new Carbon($row['date']) I get one of these errors:
DateTime::__construct(): Failed to parse time string (09/07/2014 16:55:22 MEST) at position 20 (M): The timezone could not be found in the database
DateTime::__construct(): Failed to parse time string (24/01/2014 16:57:27 MET) at position 0 (2): Unexpected character
Seems PHP does not recognize the MET/MEST timezones. How can I dynamically convert any non-standard timezones to create a Carbon/DateTime object?
The format may not be in a recognizable format. Use this instead. It will only work with MET, as MEST is not a valid timezone
$date = Carbon::createFromFormat('d/m/Y H:i:s T', $row['date']);

Categories