This question already has an answer here:
Change Date format using Carbon
(1 answer)
Closed 1 year ago.
i am using Laravel 5.8 and in database there is a column name last_seen here stored values are in that format (2021-02-05 00:42:14) i want to customise this format on listing of a page just like this (22/04/2021 03:59:54)
how can i do that ??
You can try using Carbon DateTime
Carbon::parse($user->last_visit)->format('d/m/Y H:i:s');
Include last_seen in the dates array in your model. Then you can use Carbon to format it however you like.
//in your model
protected $dates = ['last_seen'];
//controller or view
$model->last_seen->format('d/m/Y H:i:s');
To address empty/null values in last_seen
optional($model->last_seen)->format('d/m/Y H:i:s');
Use this code for changing the format of DateTime:
$datetime = "2021-02-05 00:42:14";
$datetime_format = Carbon::parse($datetime)->format('Y/m/d H:i:s'));
Output:
"2021/02/05 12:42:14"
Explanation of the formating:
H - 24 Hour with trailing zeros
i - Minutes with trailing zeros
s - Seconds with trailing zeros
For more details about the formatting of date-time please follow the link below:
PHP Date
Related
This question already has answers here:
PHP Carbon determine date type from string format
(2 answers)
Closed 1 year ago.
What method should I use to convert 2021-03-20T00:19:07.000000Z to 2021-03-20 00:19:07, in PHP and Laravel 8.x, by the way, If you can explain what does the T in the middle of 2021-03-20T00:19:07.000000Z and the dot behind each represent? I will very much appreciate you !!
You can use Carbon date library in php and laravel.
Carbon::parse('2021-03-20T00:19:07.000000Z')->format('Y-m-d H:i:s')
Your input date is an ISO 8601 formatted date.
The T is the divider between date and time, so it is a static value.
To convert your ISO 8601 date to a YYYY-MM-DD hh:mm:ss date format, you can easily use DateTime (DateTime Documentation).
So in your case the solution would be:
$input = '2021-03-20T00:19:07.000000Z';
$datetime = new DateTime(input);
$output = $datetime->format('Y-m-d H:i:s');
This question already has an answer here:
Date in a URL dd/mm/yyyy
(1 answer)
Closed 5 years ago.
I am confused as to why my date is getting converted to unix default before entry into mysql. I am sure the code is correct but cannot see why this is not working. It should convert the date that I post to script.
I would be grateful if someone could check the code and point out my error. Many thanks.
Post: 22/08/2017 05:03:29 Output:1970-01-01 12:00:00
$date = $_POST['datetimepicker'];
$parsedDate = date('Y-m-d h:i:s', strtotime($date));
d/m/Y is not one of the date formats recognized by the PHP date parser.
Given the number of digits in the date components, the parser assumes m/d/Y and because 22 is not a valid month number it fails and strtotime() returns 0.
You can use DateTime::createFromFormat() to tell the parser what format do you use:
$date = DateTime::createFromFormat('d/m/Y H:i:s', '22/08/2017 05:03:29');
echo($date->format('Y-m-d H:i:s'));
# 2017-08-22 05:03:29
This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 5 years ago.
As ive echoed out the datetime value from my db, i am now trying to display this value to edit it in the datetime-local field within my form.
The datetime vaue from db is set to:
22/3/2017 10:00:00
however, after attempting to use the following code, im left with this:
1970-01-01T01:00:00
$dat = date("Y-m-d\TH:i:s", strtotime($_GET["dat"]));
How & why is this function not working correctly to display '22/3/2017 10:00' in the form field?
Use DateTime::createFromFormat:
$date = DateTime::createFromFormat('d/m/Y H:i:s', '22/3/2017 10:00:00');
$dat = $date->format('Y-m-d\TH:i:s');
echo $dat;
Your code is not working because strtotime makes assumption based on delimiters about actual format:
m/d/Y- American format
d.m.Y or d-m-Y - European
It's not working because strtotime() can translate only specific date format.
Check the manual. For a list of supported date format, look here.
Your date format looks not included in the supported ones to me.
Examples:
strtotime("03/22/2017 10:00:00"); // Works: returns 1490173200
strtotime("22/3/2017 10:00"); // Doesn't work: returns false
You have to either change the date format in your DB or format it to one of the supported formats to make it work.
This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 8 years ago.
i have a date like this :
3/6/2014 7:20
I would like it to be formatted like
03/06/2014 07:20
What is the best practice to do that ?
As simple as :)
date("m/d/Y h:i");
Refer to Manual to see what these values represent. Um, let me bring those here.
m = Numeric representation of a month, with leading zeros
d = Day of the month, 2 digits with leading zeros
Y = A full numeric representation of a year, 4 digits
h = 12-hour format of an hour with leading zeros
i = Minutes with leading zeros
Your format is a bit ambigous, though I'm assuming since AM/PM isn't in there that the hours are in 24 hours format. Also not sure if the incoming date is day/month or month/day.
$date = new DateTime("3/6/2014 7:20");
echo $date->format('m/d/Y H:i');
Also not sure if the incoming date is day/month or month/day. But you can handle that with createFromFormat
$date = new DateTime::createFromFormat("n/j/Y G:i", "3/6/2014 7:20");
echo $date->format('m/d/Y H:i');
Read about date formats in the online docs. Also check out the DateTime book by the extension author to learn more.
Use the createFromFormat() function to read according to the specified format and output to the format you want.
http://www.php.net/manual/en/datetime.createfromformat.php
DateTime::createFromFormat -- date_create_from_format —
Returns new DateTime object formatted according to the specified format
Example:
$date = DateTime::createFromFormat('j/n/Y', '3/6/2014 G:i');
echo $date->format('m/d/Y h:i');
This question already has answers here:
Closed 11 years ago.
Possible Duplicates:
Inverse date function? - not strtotime
Is it possible to get UNIX time from such date 2011-02-27 02:04:46?
hello,
we have this function to convert a timestamp to datetime:
$datetime = date('Y-m-d H:i:s', $timestamp);
is there a function to do the opposite?
datetime to timestamp.
Thanks
$timestamp = strtotime($datetime);
Or if you're confident of the format, split up the string with explode() or even substr and pass the necessary parts into the mktime function.
Be aware that strtotime can sometimes get the timestamp wrong, if a slightly unconventional format is used.
EDIT:
A really accurate way of doing this is if you know your input format, is to use DateTime::createFromFormat eg:
$dateTimeObject = \DateTime::createFromFormat('G:i', '9:30');
$dateTimeObject->format('H:i');
See http://php.net/manual/en/function.date.php for formatting guides, and http://php.net/manual/en/datetime.createfromformat.php for info on the method described above.
$timestamp = strtotime('12-04-2010');