I have a large list of dates and times, a sample look like this...
11/22/2018 01:16:14 AM
11/23/2018 10:59:39 PM
I assume the dates are in the 'm/d/Y h:i:s A' format, I am looking for the quickest and easiest way in PHP to convert these to an epoch timestamp.
Anyone have an example?
Use strtotime function
echo strtotime("11/22/2018 01:16:14 AM");
You can do it this object oriented way:
$format = 'm/d/Y h:i:s A';
$date = DateTime::createFromFormat($format, '11/22/2018 01:16:14 AM');
echo $date->getTimestamp();
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');
I'm trying to convert the brackets to hyphens, but instead the date variable loses its value:
echo $date; // outputs 26/05/2015 10:41:56sd2
$date = date('Y-m-d H:i:s', $date);
echo $date; // outputs 969-12-31 18:00:26
The second parameter to date() must be a Unix timestamp. You're giving it a string.
That date format is invalid and won't work with strtotime() anyway. When you use / as the date separator US format is assumed. There is no 26th month.
The last three characters of that is not valid in any standard that I know of and will break any date function unless you specifically account for it (which you can't do with date() or strtotime())
Use DateTime::createFromFormat() to do this:
$date = DateTime::createFromFormat('d/m/Y H:i:s???', '26/05/2015 10:41:56sd2');
echo $date->format('Y-m-d H:i:s');
Demo
I have this line of code I wrote to create a formatted timestamp on emails from my contact us page.
It is working fine, but I'm wondering if it is written poorly and could be reduced into more efficient code? It feels wrong calling date() three times in one line. I'm not a developer by trade.
$timestamp = date('m-d-Y')." ".date('h:i A', strtotime(date('H:i:s')));
which results in: 05-28-2014 03:49 PM
Thoughts?
When you need the current timestamp, you can use,
$timestamp=date("m-d-Y h:i A");
When you need to format the timestamp you fetched from database or other means, you have to use strtotime.
$format_timestamp=date("Y-m-d H:i:s", strtotime($timestamp)); // I just convert your format to YYYY-MM-DD HH:MM:SS format.
Edit:
When you need to subtract x hours from the current time, use
$timestamp=date("m-d-Y h:i A", strtotime("-4 hour"));
Some more examples,
$timestamp=date("m-d-Y h:i A", strtotime("+2 hour")); // Adds 2 hours
$timestamp=date("m-d-Y h:i A", strtotime("+1 day")); // Adds 1 Day
You can simplify making:
$timestamp = date('m-d-Y h:i A');
or
$timestamp = gmdate("M d Y H:i:s");
For more information, see:
PHP Manual
date http://www.php.net/manual/en/function.date.php
gmdate http://www.php.net/manual/en/function.gmdate.php
This question already has answers here:
Convert from MySQL datetime to another format with PHP
(18 answers)
Closed 2 years ago.
I have a date time in a variable. My format is 08/04/2010 22:15:00. I want to display this like 10.15 PM. How to do this in PHP?
You need to convert it to a UNIX timestamp (using strtotime) and then back into the format you require using the date function.
For example:
$currentDateTime = '08/04/2010 22:15:00';
$newDateTime = date('h:i A', strtotime($currentDateTime));
$dateString = '08/04/2010 22:15:00';
$dateObject = new DateTime($dateString);
echo $dateObject->format('h:i A');
Use strtotime() to make the date a UNIX timestamp.
For output, check out the various options of date().
$timestamp = strtotime("08/04/2010 22:15:00");
date("h.i A", $timestamp);
<?php
$dateTime = new DateTime('now', new DateTimeZone('Asia/Kolkata'));
echo $dateTime->format("d/m/y H:i A");
?>
You can use this to display the date like this
22/06/15 10:46 AM
Like this:
$date = '08/04/2010 22:15:00';
echo date('h:i A', strtotime($date));
Result:
10:15 PM
More Info:
date
strtotime
for flexibility with different formats, use:
$dt = DateTime::createFromFormat('m/d/Y H:i:s', '08/04/2010 22:15:00');
echo $dt->format('g:i A')
Check the php manual for additional format options.
PHP Code:
date_default_timezone_set('Asia/Kolkata');
$currentDateTime=date('m/d/Y H:i:s');
$newDateTime = date('h:i A', strtotime($currentDateTime));
echo $newDateTime;
Output: 05:03 PM
$currentDateTime = $row['date'];
echo $newDateTime = date('l jS \of F Y h:i:s A', strtotime($currentDateTime));
Perfect answer for AM/PM live time solution
<?php echo date('h:i A', time())?>
Just simply right A
{{ date('h:i A', strtotime($varname->created_at))}}
For (PHP >= 5.2.0):
You can use DateTime class. However you might need to change your date format. Didn't try yours.
The following date format will work for sure: YYYY-MM-DD HH-MM-SS
$date = new DateTime("2010-04-08 22:15:00");
echo $date->format("g"). '.' .$date->format("i"). ' ' .$date->format("A");
//output
//10.15 PM
However, in my opinion, using . as a separator for 10.15 is not recommended because your users might be confused either this is a decimal number or time format. The most common way is to use 10:15 PM
It is quite easy. Assuming you have a field(dateposted) with the type "timestamp" in your database table already queried and you want to display it, have it formated and also have the AM/PM, all you need do is shown below.
<?php
echo date("F j, Y h:m:s A" ,strtotime($row_rshearing['dateposted']));
?>
Note: Your OUTPUT should look some what like this depending on the date posted
May 21, 2014 03:05:27 PM