How to convert date from "2011-08-01 04:01:45" to "Mon, 01 Aug 2011 05:37:45 -0400" in Php
Thanks a lot...
$newDate = date('r', strtotime('2011-08-01 04:01:45'));
use the strtotime function in php to parse the date string and then the date function to print it in whatever format you'd like.
<?php
$orig = "2011-08-01 04:01:45";
$timestamp = strtotime($orig);
echo date("D, d M Y H:i:s O",$timestamp);
?>
I know that this isn't going to solve your problem but a really fast method of using dates and information is to store the time using a unix time stamp. This is an integer value of seconds counting from the time (1970 -1 second).
It will allow you to format dates in a much simpler manner using something like:
<?php date('d-m-Y', $unixTimeStamp); ?>
This method is also much faster when it comes to comparing times.
Related
I have a date in this format "Wed, 26 Oct 2022 16:11:30 -1100", need to convert it to UTC time and in a format so it can be inserted into a MySQL database.
The date was pulled from an email using "$headerInfo->date;". I don't see any way to receive the date any different.
Every way I try to do the conversion is painful and brute force.
Is there an elegant, or not painful way to do this?
TIA.
Been trying regex but it doesn't handle converting the month into digits, then you have the UTC offset (time zone) to work with.
Parse the date/time string using DateTimeImmutable::createFromFormat, set the timezone to UTC then format it to MySQL's datetime literal syntax
$dt = DateTimeImmutable::createFromFormat('D, j M Y H:i:s O', $dateString);
$mysqlFormat = $dt->setTimezone(new DateTimeZone('UTC'))->format('Y-m-d H:i:sP');
Demo ~ https://3v4l.org/G7RBG
<?php
//just use strtotime
echo date('Y-m-d H:i:s e',strtotime('Wed, 26 Oct 2022 16:11:30 -1100'));
?>
I am getting Google Analytics data via API using google-api-php-client
Everything fine except one thing, I can't convert day timestamp into readable value. The day timestamp looks like 20150724, date('M j', $date); always shows Aug 22 even for different timestamps.
How to fix it?
If you don't tell PHP what the number is, it will assume it's a UNIX timestamp. 20150724 is 22nd August 1970...
So just tell it how it's formatted:
<?php
$google_date = '20150724';
$date = DateTime::createFromFormat('Ymd', $google_date);
echo $date->format('M j');
date() expects the $date to be a timestamp which is the number of seconds since the Unix epoch. Try converting $date with strtotime.
I am receiving JSON data with a date string with this format:
'Mon Jun 30, 2014'
What would be the way to convert this to a datetime? Checking the PHP functions for this I got unsecure if better to use date_parse_from_format or date_create_from_format.
What are the differences and which would suit better for this task?
DateTime::createFromFormat would work well here. It allows you to format the string easily and also takes timezones into consideration when appropriate and can be easily used in comparisons without having to convert to a timestamp first.
$date = DateTime::createFromFormat('D M d, Y', 'Mon Jun 30, 2014');
echo $date->format('Y-m-d');
You can convert to a unix timestamp using strtotime(). I don't know what you mean by a "datetime", but if you mean something like for MySQL then you format the timestamp with date() (you can include time but it isn't present in the original string):
echo date('Y-m-d', strtotime($string));
The second of the two likely fits you better ---
The first one only breaks down the date into an array, so you can work with the parts, individually.
But the second returns the DateTime object you are looking for.
I have a string that is a 10 digit date/time representation in the format of yymmddhhmm, e.g:
1304282240
is the 28th of April, 2013 22:40 (28/04/13 in the DD/MM/YY format)
I need to convert this into a valid PHP timestamp but I'm getting stuck using the Date and strtotime functions, and not sure if that is the best approach in any case.
Update: I've added these lines to my code:
$date = DateTime::createFromFormat('ymdHi', '1304282240');
echo $date->format('m/d/Y h:i:s A');
appears to work
I believe you want something like this:
$date = DateTime::createFromFormat('ymdHi', '1304282240');
I'm trying to convert a time format in the format below to a unix timestamp using PHP
j n Y H:i:s
Im trying to find a way to convert to a unix timestamp so it can be used in SQL databases. An example of the dates that I need to convert:
28 Mar 12 16:37:34
I've tried functions called "strptime" and "mktime" that I found on stackoverflow to no success - im not really sure what Im doing with them. If this is the answer here, could someone explain how to use them? Ive tried to understand the PHP documentation but Im just not getting it.
The post I was reading is here: PHP date format converting
echo strtotime('28 Mar 12 16:37:34'); //1332945454
http://php.net/manual/en/function.strtotime.php
If you need ultimate flexibility on parsing the format, use DateTime::createFromFormat()
$dt = DateTime::createFromFormat(
'j M y H:i:s', $dateString, new DateTimeZone('Your/Timezone'));
$timestamp = $dt->getTimestamp();
The php way is to use date() and strtotime()
sql uses YYYY-MM-DD HH:MM:SS
$dateTime = date('Y-m-d H:i:s', strtotime('28 Mar 12 16:37:34'));