String to time PHP - php

How to convert "00:00:00.000 GMT Mon Nov 22 2010" to more user friendly format.

You could use these PHP functions:
http://php.net/manual/en/function.strtotime.php
http://php.net/manual/en/function.date.php
strtotime will convert your current to a unix timestamp.
You can then format that unix time stamp however you want, using date().

Why not with strtotime?!
echo date('d/m/Y',strtotime('0:00:00.000 GMT Mon Nov 22 2010')); // I'm french

You might also find these useful:
$mysqldate = date( 'Y-m-d H:i:s', $phpdate );
$phpdate = strtotime( $mysqldate );
And in CakePHP the TimeHelper http://book.cakephp.org/view/1470/Time:
echo $time->nice($mysqlDate);
etc.

Try this:
//initial format
echo '0:00:00.000 GMT Mon Nov 22 2010';
//unix format: 2010-11-22
echo date('Y-m-d',strtotime('0:00:00.000 GMT Mon Nov 22 2010'));
//explicit format: Monday 22 November 2010
echo date('l d F Y',strtotime('0:00:00.000 GMT Mon Nov 22 2010'));

First convert the string to a datetime using strtotime:
strtotime
and then format the datetime anyway you want using sprintf:
sprintf

Use strtotime function:
echo strtotime("00:00:00.000 GMT Mon Nov 22 2010"), "\n";

check on php manual and try strtotime and date function

Related

What create_from_format to use for timestamps in php

I am working on a PHP file were I am pulling in timestamps from a database that come out as
Tue Sep 22 16:11:12 EDT 2015
Mon Nov 16 07:42:31 EST 2015
I was wondering how to convert dates of this format into unix time format.
You can use the DateTime object
$dateTime = new DateTime('Tue Sep 22 16:11:12 EDT 2015');
echo $dateTime->getTimestamp();
The result would be 1442952672
If you look at the PHP manual for \DateTime::createFromFormat() or date(), you will find tables of codes accepted by \DateTime::createFromFormat(). Looking at them you will see that you need the following:-
$date = \DateTime::createFromFormat('D M d H:i:s T Y', 'Tue Sep 22 16:11:12 EDT 2015');
echo $date->getTimestamp();
Demo.
The 'd' in the format string may need to be a 'j' if day numbers < 10 are single digit.

Convert a datetime format to match MySQL's datetime with PHP

I need a way to convert this format Tue, 20 Oct 2015 17:43:23 0000
to the datetime format that MySQL accepts.
I have tried to convert it with strtotime() but the function returns a negative number.
The problem appears to be the trailing zeroes, by trimming them from the date string it seems to work fine. The code below uses one of the Date constants - change that for the format desired.
$d='Tue, 20 Oct 2015 17:43:23 0000';
echo date( DATE_COOKIE, strtotime( trim($d,' 0000') ) );
outputs -> Tuesday, 20-Oct-15 17:43:23 BST
echo date( 'Y-m-d H:i:s', strtotime( trim($d,' 0000') ) );
outputs -> 2015-10-20 17:43:23
Try this with your time specified as the $inputtime variable:
strftime("%F %H:%M:%S", strtotime($inputtime))
The problem lies in your time format Tue, 20 Oct 2015 17:43:23 0000. Here 0000 means time zone. But it should have format + or - before value to work properly. Look at example:
echo strtotime("Tue, 20 Oct 2015 17:43:23 +0000");
Try this
$date = \DateTime::createFromFormat('r',your date);
$date = $date->format(format that you need);

PHP Date time conversion to save to MySQL

I need to get data from RSS feed and then save it to MySQL. The problem is that in RSS feed datetime format is like this: Sun, 09 Nov 2014 12:00:38 +0200 How I could convert it to format so I could save it to database? and how to convert it back later when I want to display it again with the same format?
Try this
$DateTime= date("Y-m-d H:i:s", strtotime("Sun, 09 Nov 2014 12:00:38 +0200"));
echo $DateTime;
To retrieve back from db, in your select query use
DATE_FORMAT(date_column, '%a %d %b %Y %T')
if you are using PHP along with MySQL, strtotime() is nice php function :)
http://php.net/manual/en/function.strtotime.php
date_default_timezone_set('UTC');
$date_string = 'Sun, 09 Nov 2014 12:00:38 +0200';
echo 'original string: '.$date_string.'<br/>';
$unix_time_stamp = strtotime($date_string );
echo 'timestamp: '.$unix_time_stamp.'<br/>';
$old_format = date("D, j M Y H:i:s O", $unix_time_stamp );
echo 'back to originalt: '.$old_format;
Example on http://viper-7.com/KI5LfG
You can get any date format you desire with php date()
http://php.net/manual/en/function.date.php

Date is not stored correctly in a cookie

I stored this date in a cookie:
Tue Jun 26 2012 15:38:41 GMT+0200 (CEST)
and now I need to print it in this form:
06/26/2012 15:38
So I write this code:
echo $_COOKIE['date'];
echo date('m/d/Y g:i A', strtotime($_COOKIE['date']));
but I get this:
Tue Jun 26 2012 15:38:41 GMT 0200 (CEST)
12/31/1969 7:00 PM
instead of:
06/26/2012 15:38
Why?
strtotime does not seem to understand the date format, you can try to parse it with DateTime::createFromFormat or you can try to store it in another format
Perhaps its easier to store the timestamp in the cookie and not the date string.
$_COOKIE['date'] = time();
and then read it direct to the date function.
i have tried this:
<?php
setcookie('date', time());
var_dump($_COOKIE);
echo date('m/d/Y g:i A', $_COOKIE['date']);
?>
and the result is: 06/25/2012 6:13 PM
Because strtotime is not recognizing the date format you are using.

Timestamp to normal date format and reverse conversion using PHP

I have a timestamp as 2011-08-27 18:29:31. I want to convert it to 27 Aug 2011 06.29.31 PM. Also, I want to convert this format reverse back to the previous timestamp format.
How van I do this using PHP?
$converted = date('d M Y h.i.s A', strtotime('2011-08-27 18:29:31'));
$reversed = date('Y-m-d H.i.s', strtotime($converted));
Don't use date()! It`s too old function. In PHP v. 5.2 and more you should use date_format or DateTime::format object.
you can use the date_format() function
//Convert to format: 27 Aug 2011 06.29.31 PM
$converted_date = date_format('d M Y h.i.s A',strtotime($orig_date));
//Convert to format 2011-08-27 18:29:31
$converted_date = date_format('Y-m-d H:i:s',strtotime($orig_date));
Have you looked at the date functions in PHP, especially strftime (for formatting a timestamp) and strtotime
To convert from 2011-08-27 18:29:31 to 27 Aug 2011 06.29.31 PM:
echo date('d M Y, H.i.s A', strtotime('2011-08-27 18:29:31'));
To do the reverse:
echo date('Y-m-d H:i:s',strtotime('27 Aug 2011 06.29.31 PM'));
If that doesn't work, you may have to try:
$date = date_create_from_format('d M Y, H.i.s A', '27 Aug 2011 06.29.31 PM');
echo date_format("Y-m-d H:i:s",$date);

Categories