How to get date format of given string, string contain a valid date
I am try to import data from csv file, and that csv files are exported for backup
but there are so many different type of date format and in some case when i try to convert string to date it throwing me an error
ex: 04/08/2010 10:22 am
some time this type of format throwing me error
Error 500: DateTime::__construct(): Failed to parse time string
You can use just strtotime
echo date('Y-m-d H:i:s', strtotime('04/08/2010 10:22 am')); // output: 2010-04-08 10:22:00
You can use the DateTime::createFromFormat() static method.
Also, you may want to do some reading:
DateTime::__construct() reference
Supported Date and Time Formats
Date/Time extension reference
Also, you need to use the search function (top right on the page). There are a lot of questions that deal with similar problems.
Try this...........
$today = date("d/m/Y g:i a");
<?php
$today = date("d/m/Y g:i a");
echo $today;
?>
You will get output like this.........20/02/2013 7:53 am
For sample one see this example link
Related
I am using Laravel and I have to get some dates and store it on MySQL database.
When I create the date like this:
$date_sol = Carbon::createFromFormat("Y-m-d H:i:s","2020-12-10 01:00:00");
The date is properly stored on the database. However, I have to get the date from an input.
I am trying to get the date and then format it like this:
$novaData = $request->input('solicitacao_data') . ' 15:16:17';
$sol->data = Carbon::parse($novaData)->format("Y-m-d H:i:s");
However, I get the error:
DateTime::__construct(): Failed to parse time string (28/03/2020
15:16:17) at position 0 (2): Unexpected character
The error is at the line $sol->data = Carbon::parse($novaData)->format("Y-m-d H:i:s");
How do I make the formating conversion properly? I am new using Laravel. I am not sure about it.
For date format 'd/m/Y' try this.
Carbon::createFromFormat('d/m/Y', '22/02/2020')->toDateTimeString();
Similarly for date format Y-m-d try this
Carbon::createFromFormat('Y-m-d', '2020-02-22')->toDateTimeString();
output will be in format (Y-m-d H:i:s)
"2020-02-22 21:05:13"
Let's say you receive something as input.
Well, ideally you should first sanitize it, to make sure you received a string that can be interpreted as a date. For that, I would suggest you to have a look there :
php date validation
So, you assign the input to a var and append a string representing some time to it:
$novaData = $request->input('solicitacao_data'). ' 15:16:17';
From here, the easiest is to convert the string into a timestamp. Which can be achieved this way:
$time = strtotime($novaData);
And now, you can use Carbon to format the date the way you want :
$sol->data = Carbon::createFromTimestamp($time)->format("Y-m-d H:i:s");
I tried to use strtotime function to format the today's date in PHP but its giving me the wrong result. My code is given below.
<?php
$today = date("m-d-Y H:i:s");
echo date('m-d-Y H:i:s', strtotime($today));
?>
Here, I am getting this 01-01-1970 05:30:00 result.
Here, I need to get the proper datetime result.
date("m-d-Y") is what's causing issues for you. For example, take 01-02-2019 and 02-01-2019 - which is Februrary 1st and which is January 2nd? That format will make strtotime() return false, as it doesn't know what format that is for days that are greater than 12.
d-m-Y would be expected and a valid format.
You can use DateTime::createFromFormat() instead. Then you can create a valid DateTime object from that format, and use it however you need it to.
$today = DateTime::createFromFormat("m-d-Y H:i:s", date("m-d-Y H:i:s"));
echo $today->format("m-d-Y H:i:s");
Live demo
Documentation for DateTime::createFromFormat()
Alternatively, if you just need to print the date directly and not process it further, you don't need to go through any hoops and can just use date() as you were, without the second line. But you can not use that result in a strtotime() function, as it will return incorrect results.
echo date("m-d-Y H:i:s");
I have gone through convert php date to mysql format but still I have few issues and questions,
In general, my application, can input dates in Y-m-d, d-m-y, m-d-Y, jS, F Y format, or even with '/' separator.
I want this dates to be converted in to MYSQL Y-m-d format,
I tried as below, but it shows different output than expected,
Non working
date_format(date_create_from_format('d-m-Y', '02-25-2016'), 'Y-m-d');
Working
date_format(date_create_from_format('d-m-Y', '25-02-2016'), 'Y-m-d');
So it seems format and string to be match in same way, otherwise its not interpreting correctly,
What is best way to convert above 4 format inputs to mysql(Y-m-d) format?
Thanks advanced,
I'd recommend using Carbon for any date processing in PHP these days.
Creating date by parsing strings is simple enough...
http://carbon.nesbot.com/docs/#api-instantiation
Getting the output in a format you need is also simple...
http://carbon.nesbot.com/docs/#api-formatting
I'd suggest you to use this function: DateTime::createFromFormat
Using this, you need to create a date by specifying the particular format.
Try this:
$date = DateTime::createFromFormat('Y-m-d', '2016-05-26');
echo $date->format('Y-m-d');
echo "<hr/>";
$date = DateTime::createFromFormat('d-m-Y', '26-05-2016');
echo $date->format('Y-m-d');
echo "<hr/>";
$date = DateTime::createFromFormat('m-d-Y', '05-26-2016');
echo $date->format('Y-m-d');
echo "<hr/>";
$date = DateTime::createFromFormat('jS F Y', '26th May 2016');
echo $date->format('Y-m-d');
Thanks for all suggestions, i think i found my logic,
My date format selection is stored in sql db, so will try as below,
date_format(date_create_from_format('format from db', 'user entered format'), 'Y-m-d');
As user entered format is stored in db, so i will pull that in this date_format function, so both will match and i can convert to Y-m-d!
Thanks,
I have a $date and $time fields that are stored in my db, I would like to know how can I format these fields to display them in a different format?
e.g.
2010-05-09 -> 9th May 2010
and
13:00:00 -> 1:00PM
I have tried to use carbon but it returns the following error:
InvalidArgumentException in Carbon.php line 414: Unexpected data
found. Data missing
You can always use PHP's strtotime() function and then manipulate the structure using date() as seen below.
$dateString = strtotime("2010-05-09");
$date = date("jS F, Y" ,$dateString);
echo $date;
$timeString = strtotime("13:00:00");
$time = date("g:i A" ,$timeString);
echo $time;
FWIW, I didn't realize you said specifically in Laravel. But I typed it all out, so figured I'd share anyways. Carbon (as mentioned above) may handle this in a similar fashion.
You have to use the createFromFormat method:
$time = Carbon::createFromFormat('H:i:s', '13:00:00')->format('g:iA');
The first argument is the format of the date in your string.
i have a date string from my db, the date is 16/11/2010 and its format is d/m/Y, i want to modify its like this.
<?php
$date_from_db= '16/11/2010'; // format is d/m/Y
$date = new DateTime($date_from_db);
$date-> modify('+1 week');
echo $date-> format('d/m/Y') ;
?>
i have got this error
Fatal error: Uncaught exception 'Exception' with message 'DateTime::__construct(): Failed to parse time string (16/11/2012) at position 0 (1).
How can i fix this?
try
$date = DateTime::CreateFromFormat("d-m-y", "16-11-2010");
Unfortunately that is not one of the time formats supported by DateTime class constructor.
This page shows valid formats
http://www.php.net/manual/en/datetime.formats.date.php
You should use an actual SQL-compliant date field (typically YYYY-MM-DD) in your DB. Or if you can't change the way your are storing the dates, use DateTime::CreateFromFormat as suggested in other answer.
I would highly suggest using a more standard storage format though.