Converting this date format to Unix Time in PHP - php

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'));

Related

Convert a date/time so it can be inserted into MySQL in PHP

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'));
?>

RFC-822 DateTime Formatting in PHP with Database [duplicate]

This question already has answers here:
Conversion from MySQL date to RFC822 date format
(2 answers)
Closed 9 months ago.
I have a date field on my database. the date format as following.
June 17, 2013
Im using the format as
date("F j, Y");
So my question is there a way that i can display this date in RFC-822 format using php? or do i need to start saving the date in RFC-822 format from now on? Thanks in advance.
Using the following syntax, you can display current time in RFC822 Format.
$date = new DateTime('2000-01-01');
echo $date->format(DateTime::RFC822);
Neither.
From now on you have to start using format supplied by database.
You have to understand the difference between storage format and display formatting. It's different matters. When storing data in mysql, you have to follow mysql rules. So, instead of June 17, 2013 you have to store 2013-06-17.
And then convert at output to whatever format required - not limited to a single one but whatever format is demanded by destination.
None of the other answers worked for me, so this is what worked... to take a date in PHP and output it in RFC822:
date("D, d M Y G:i:s T", strtotime($date));
Hope that helps others.
As was pointed out your best bet is to change the way you are storing your dates to something other then a string. date("Y-m-d", strtotime($date)) can assist you in this endeavor.
But to solve the immediate need you can utilize use strtotime, date and the DATE_RFC822 constant to get you what you are looking for.
echo date(DATE_RFC822, strtotime($value));
See First example on php date documentation
As #ashleedawg and others mentioned in some comments the simplest solution that works:
date("D, d M Y H:i:s O", strtotime($date));
Mind the "H" and the "O" ;)
Thanks!
If you want to date format something in PHP for RFC-822 , then just do this...
date('r', strtotime($date))
'r' ยป RFC 2822 formatted date Example: Thu, 21 Dec 2000 16:01:07 +0200
Source: PHP.net: DateFormat
But, as other stated, you don't want to store this in your database! However, you'll need to use r for other things, like XML-RSS date time formats...
All date-times in RSS conform to the Date and Time Specification of RFC 822... (Source: RSS 2.0 Specification.)
date_format(date(your database field), '%D, %j %M %t')
and what type of format you want just see the link
date and time format for Mysql
You can save it as TimeStamp in database and show it RFC822 format
date(DATE_RFC822, time());
This is the only solution that worked for me:
date("D, d M Y H:i:s T", strtotime($date));
Other examples above that didn't work include using the DATE_RFC822 format specifier, which puts out a 2-digit year, instead of 4 digits. Then the other suggestion to use G:i:s for time doesn't work because G specifies no leading zeroes, so you'll get 2:00:00 instead of 02:00:00.
don't use T at the end but an "O", it works for me

How to convert date from one format to another in Php

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.

Creating date in PHP

I need to convert this date:
10.04.2011 19:00
To a date variable that I can use in PHP.
Can someone help me with that? I tried this way:
$dateConverted = date("d.m.Y H:i",strtotime ($date));
But it returns 01.01.1970 00:00
DateTime::createFromFormat() to the rescue!
It looks like your format is d.m.Y H:i.
So, this should work for you:
$dt = DateTime::createFromFormat('d.m.Y H:i', '10.04.2011 19:00');
echo $dt->format('Y-m-d H:i:s');
You should also take a look at the formats that strtotime and DateTime operate on. In particular, the reason that date didn't parse in strtotime is that it only expects dots as delimiters between Y, M and D if the year is only two digits. That's an odd one, don't look at me, it's not my fault.

PHP: convert date into seconds?

I've a date like Tue Dec 15 2009. How can I convert it into seconds?
Update:
How can I convert a date formatted as above to Unix timestamp?
I assume by seconds you mean a UNIX timestamp.
strtotime() should help.
You can use the strtotime function to convert that date to a timestamp :
$str = 'Tue Dec 15 2009';
$timestamp = strtotime($str);
And, just to be sure, let's convert it back to a date as a string :
var_dump(date('Y-m-d', $timestamp));
Which gives us :
string '2009-12-15' (length=10)
(Which proves strtotime did understand our date ^^ )
[edit 2012-05-19] as some other questions might point some readers here: Note that strtotime() is not the only solution, and that you should be able to work with the DateTime class, which provides some interesting features -- especially if you are using PHP >= 5.3
In this case, you could use something like the following portion of code :
$str = 'Tue Dec 15 2009';
$format = 'D F d Y';
$dt = DateTime::createFromFormat($format, $str);
$timestamp = $dt->format('U');
DateTime::createFromFormat() allows one to create a DateTime object from almost any date, no matter how it's formated, as you can specify the format you date's in (This method is available with PHP >= 5.3).
And DateTime::format() will allow you to format that object to almost any kind of date format -- including an UNIX Timestamp, as requested here.
You mean like an UNIX-timestamp? Try:
echo strtotime('Tue Dec 15 2009');

Categories