How do I convert the following to insert into mysql? - php

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;

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 get date from string

I'm from mail source I get dates like: Mon, 22 Jul 2013 09:08:50 +0200
I must get from this dates date: 2013-07-22.
I try this
$date = "Mon, 22 Jul 2013 09:08:50 +0200";
date("Y-m-d", strtotime($date))
What I do wrong ?
You need to echo the resulting value. Also, note that the output will be affected by the time zone on the machine running the PHP script.
Use like this
$date = "Mon, 22 Jul 2013 09:08:50 +0200";
echo date("Y-m-d", strtotime($date));

Format a date for PHP [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 8 years ago.
I have a date:
Mon, 31 Mar 2014 12:19:10 GMT
How can I convert that to this format:
date('Y-m-d H:i:s')
I have found something like this:
$time = strtotime('10/16/2003');
$newformat = date('Y-m-d',$time);
But how do I convert 'Mar' to '03', so I can use that?
How about using DateTime
$date = new DateTime("Mon, 31 Mar 2014 12:19:10 GMT");
echo $date->format("Y-m-d H:i:s");
No need, PHP's strtotime understands month names.
C:\Users\Niet>php
<?php
var_dump(date('Y-m-d H:i:s',strtotime('Mon, 31 Mar 2014 12:19:10 GMT')));
^Z
string(19) "2014-03-31 12:19:10"
strtotime does the trick here too.
This code:
$date = 'Mon, 31 Mar 2014 12:19:10 GMT';
echo $date;
$time = strtotime('Mon, 31 Mar 2014 12:19:10 GMT');
echo date('Y-m-d H:i:s', $time);
will output this:
Mon, 31 Mar 2014 12:19:10 GMT
2014-03-31 14:19:10
Note that your timezone is important here too. I'm in GMT+2 timezone, so final hour is 14 instead of 12.

Formatting date and time to get date, month, year each in seperate var

In my json response of twitter API I get time stamp like this
Thu Mar 13 14:24:13 +0000 2014
I tried to format in this way:
$created_at = $thing->created_at;
$date = DateTime::createFromFormat('D M d H:m:s O Y', $created_at);
echo $created_at;
echo $date->format('H:m:s');
Which gives result like this:
Thu Mar 13 14:24:13 +0000 2014
2015:12:13 //formated result. How come 2015?????
Wed Mar 12 14:18:14 +0000 2014
2015:06:12
Tue Jan 21 12:50:17 +0000 2014
2018:02:21
Thu Dec 12 09:29:16 +0000 2013
2015:05:12
Why giving wrong result?
I want to get month, year in seperate variable.
You can simplify the creation of the DateTime by doing this:
$dt = new DateTime('#' . strtotime('Thu Mar 13 14:24:13 +0000 2014'));
This parses the date string to a Unix timestamp, and then creates a DateTime object.
echo $dt->format('Y-m-d H:i:s'); // yields the correct result.
You are using month format character m instead of minutes i, thats why you get "wrong" output.
$dt = new DateTime('Thu Mar 13 14:24:13 +0000 2014');
echo $dt->format('H:i:s');

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.

Categories