Date is not stored correctly in a cookie - php

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.

Related

how can I format my date in php to have the query new Date() format?

I have a php script that returns the date of the server:
<?php
echo date('D, d M y H:i:s a');
?>
but when I print this value on the client site I get:
Tue, 29 Sep 15 16:19:28 pm
But instead I need the date in this format:
Tue Sep 29 2015 16:18:00 GMT+0200 (Central Europe Daylight Time)
How should I modify my php script then to have it like this?
Thanks!
echo (new DateTime())->format('r');
$datestring = "26-08-2015 03:35:28"; //date as string
$date = new DateTime($datestring); //String to datetime conversion
$date = $date->format('D d M y H:i:s O e'); //format the date
echo $date;
This is the manual link. Timezone you have to set.
Output will look like Wed 26 Aug 15 03:35:28 +0200 Europe/Paris
Go to the very bottom of this page http://php.net/manual/en/function.date-default-timezone-set.php

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

How do I convert the following to insert into mysql?

I have the following date/time: Thu, 26 Jun 2014 07:13:32 +0000
I need to convert this to the format for inserting into mysql: 2014-06-26 07:13:32
How would I go about doing so?
I tried doing date("Y-m-d H:i:s", strtotime(Thu, 26 Jun 2014 07:13:32)) but it gives me the wrong time (specifically hour).
This code works fine for me and gives the result you specify above:
<?php
date_default_timezone_set("UTC");
print date("Y-m-d H:i:s", strtotime("Thu, 26 Jun 2014 07:13:32"));
Its because of the time zone causing the time to change try this:
<?php
date_default_timezone_set("UTC");
echo date("Y-m-d H:i:s",strtotime('Thu, 26 Jun 2014 07:13:32 +0000'));
You can convert the date to the desired format easily using DateTime class
$date = new DateTime('Thu, 26 Jun 2014 07:13:32 +0000');
$modified_date=$date->format("Y-m-d H:i:s");
echo $modified_date;

Javascript time to PHP time

I have a time value in javascript for example 11:30:00.
Date {Mon Oct 22 2012 11:30:00 GMT+0800 (Taipei Standard Time)}
and I passed to a php file and converted it by:
$newTime = date('H:i:s', $theTime);
But, it return 05:36:00. What is the right way of concerting time?
Use myDate.getTime() instead, and then divide this by 1000 since PHP deals with seconds while JavaScript deals with milliseconds.
If you're looking to use PHP to parse the date/datetime, then you should use strtotime(). Something like:
$time = "Mon Oct 22 2012 11:30:00 GMT+0800";
echo date('Y-m-d h:i:s', strtotime($time));
Which would output:
2012-10-22 04:30:00
This output is GMT, which you can change if required.
EDIT:
If you're expecting 11:30:00, then try the following:
date_default_timezone_set('UTC');
$time = "Mon Oct 22 2012 11:30:00 GMT+0800";
echo date('Y-m-d h:i:s', strtotime($time));

String to time 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

Categories