PHP: convert date into seconds? - php

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

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

Converting a string to Date

I have a string in the format Apr 27 2014 and I need to convert to a date in the format yyyy-mm-dd. Is there a easy straight forward way to accomplish that? I couldn't find anything besides write a little parser.
This should work for you:
$date = new DateTime("Apr 27 2014");
echo $date->format('Y-m-d');
Output:
2014-04-27
For more information about the DateTime class see the manual: http://php.net/manual/en/class.datetime.php
You can also use strtotime() and date() functions:
<?php
echo date("Y-m-d", strtotime('Apr 27 2014'))."\n";
You can look at other examples with strtotime() at php.net.
Notice: unix timestamp is limited on 32bit systems form ~1900 till 2038.

PHP convert string with weeks and short month name to date

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.

Convert yymmddhhmm date/time string to PHP timestamp

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

Past textual date to days (since) PHP

Hey guys,
how does one calculate the days past since a date like the one Twitter outputs in its API
eg:
Mon Jul 12 00:27:26 +0000 2010
to XXX
Can we do it with strtotime
Thanks guys,
Dex
Compatibility note: works only for PHP >= 5.3.0
Providing that the date format does not change, you can reverse the process (i.e. reverse timestamp -> string (on Twitters servers) to timestamp) using the exact date format. Using the table on the manual page of DateTime::createFromFormat:
<?php
$str = 'Mon Jul 12 00:27:26 +0000 2010';
$oDateTime = DateTime::createFromFormat('D M d H:i:s T Y', $str);
$unix_timestamp = $oDateTime->getTimestamp();
echo $unix_timestamp;
?>
Beware: On my machine, date('d-m-Y H:i:s', $unix_timestamp) differs two hours, the machines timezone is GMT+2.
To calculate the difference between in days between two Unix timestamps, use math (a day has 86400 seconds):
echo ($unix_timestamp1 - $unix_timestamp2) / 86400;
If you've two such dates, you can use DateTime::diff as suggested in the comments by Zerocrates. You've create two DateTime instances using DateTime::createFromFormat and invoke the DateTime::diff with two arguments passed, previously created DateTime instances. The returned DateInterval instance has a d property which contains the difference in days.
The other way would be using the getTimestamp method, doing the maths from the previous example.
References:
http://php.net/manual/en/datetime.createfromformat.php
http://php.net/manual/en/datetime.gettimestamp.php
http://www.php.net/manual/en/datetime.diff.php
http://www.php.net/manual/en/class.dateinterval.php
You can do it like that (where $your_date is the string you mentioned):
$diff = (time() - strtotime($your_date)) / (24*60*60);
In your case, when I did echo $diff; the output was (at the time I posted the answer):
321.85475694444
See more details for strtotime(), time() and date().
Hope this helped you.
You might find sth here:
performing datetime related operations in PHP
or in php manual there is a lot..
http://php.net/manual/en/function.date.php

Categories