Laravel - Carbon Convert Meridian Time to DateTime - php

I have this DateTime format.
2016-11-22 12:04 PM
I want to convert it to UTC datetime which can be inserted into MysQL.
Can somebody please tell how to do it with carbon?

Set your timezone and add this code something like this.Hope this will be helped you.
$timestamp = '2016-11-22 12:04 PM';
$date = Carbon::createFromFormat('Y-m-d H:i A', $timestamp, 'America/New_York');
$date->setTimezone('UTC');

Related

wrong convert date() PHP

I used date() to convert 12H time to 24H using this code
$over = date("Y-m-d H:i:s", strtotime("2021-12-16 13:42:46 PM"));
echo $over;
but the output is this below:
1969-12-31 16:00:00
How to get rid of this, is this a bug? or my code?
sandbox
13:42:46 PM isn't 12h time format (PM is nonsense in 24h format), 01:42:46 PM is correct.
Just specify the correct date format (PM is not supported):
$over = date("Y-m-d H:i:s", strtotime("2021-12-16 13:42:46"));
echo $over;
Which date format is supported you can find in https://www.php.net/manual/de/datetime.formats.php

DateTime formate in php

I have date stored in database in this format frankly i dont know the timezone of this format
2016-05-26T11:35:00.000Z
but i want to display date in this format
May 27, 2016 12:00 am
Here is code
$date = substr($query_result->post_date, 0,10);
$date = date_create($date);
date_format($date,"F j, Y g:i a");
It shows date correctly but it always shows 12:00 am with date.
Like if date is
2016-05-26T11:35:00.000Z
then it should display
May 26, 2016 11:35 am/pm
Thanks in advance
While shrinking your datetime string, you lose time and timezone information. Time 00:00 and system timezone is then applied to your datetime, and you get wrong result because of that.
$date = date_create($query_result->post_date);
date_format($date,"F j, Y g:i a");

Get timestamp of beginning of day given 'Y-m-d'

Given a date string formatted Y-m-d (2014-4-11 for example), how can I get a UNIX timestamp of 12am the beginning of that day? Would it involve date_parse_from_format()?
Thanks
You can simply use strtotime()
$date = "2014-04-11";
$timestamp = strtotime($date);
which inturm gives you -
$d = date('Y-m-d H:i:s', $timestamp); // 2014-04-11 00:00:00
Try online conversion to test - http://www.onlineconversion.com/unix_time.htm

PHP convert separate date and time strings into single datetime

I have two separate strings for date and time formatted something like: 14/04/2014 and 01:15 PM
I would like to convert these to a datetime formatted as Y-m-d H:i:s
Any idea how I go about this??
$date = DateTime::createFromFormat('d/m/Y h:i A', '14/04/2014'.' '.'01:15 PM');
echo $date->format('Y-m-d H:i:s');
Demo

Convert Time to Time Stamp PHP

I have a string :
$Value = 03/25/2014 10:15 AM
I would like to convert this to a mysql timestamp. When I use date('Y-m-d H:i:s', $Value) it does not work. Can someone please help me convert this? Thank you.
date needs a UNIX timestamp:
$Date = date('Y-m-d H:i:s', strtotime($Value));

Categories