PHP get date from string - php

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

Related

PHP Date and Time and timezone

Is there any php function that would convert a date string to its equivalent Asia/Manila time.
I tried setting Asia Manila as my default timezone but no avail.
Please see sample below that needs to be convert to Asia/Manila Time
Sun, 12 Jan 2015 08:27:42 +0000,
Mon, 12 Jan 2015 00:14:04 -0500,
Mon, 12 Jan 2015 05:13:34 +0000 (UTC),
Mon, 12 Jan 2015 08:57:47 +0000 (UTC),
Tue, 13 Jan 2015 01:38:04 +0700 (WIT),
Tue, 13 Jan 2015 00:47:31 +0900 (JST),
Mon, 12 Jan 2015 23:27:26 +0000
your assistance is highy appreciated..
Thanks in advance.
This is the easiest way
date_default_timezone_set('Asia/Manila'); // Set your default TZ to Asia/Manila
// strtotime() will convert all timezones to your default
echo date('Y-m-d H:i:s' , strtotime('Mon, 12 Jan 2015 05:13:34 +0000 (UTC)'));
Try this
$date = new DateTime('2000-01-01', new DateTimeZone('Asia/Manila'));
echo $date->format('Y-m-d H:i:sP') . "\n";
Please try the following
$sdt = '2012-05-15 10:50:00';
$stz = new DateTimeZone('UTC');
$dtz = new DateTimeZone('Asia/Manila');
$dt = new DateTime($sdt, $stz);
$dt->setTimeZone($dtz);
$ddt = $dt->format('Y-m-d H:i:s');
Hope this wil help you.
function convert_timezone($from_tz, $to_tz, $time = '2008-08-03 12:35:23') {
date_default_timezone_set($from_tz);
$datetime = new DateTime($time);
$time_newTZ = new DateTimeZone($to_tz);
$datetime->setTimezone($time_newTZ);
$newDate = $datetime->format('Y-m-d H:i:s');
return $newDate;
}
echo convert_timezone('Asia/Kuala_Lumpur', 'America/Los_Angeles');

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;

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

Using PHP strtotime() function

I'm getting an external RSS Feed Post in different website, what I noticed is that they all have a different format of Publish Date.
Some uses a formatting like this:
2012-04-09T08:23:07Z
Fri, 06 Apr 2012 01:25:43 0000
There are still lots of format but I was just showing you two examples.
Now, I want everything to have a uniform format, so i'm using strtotime(). Here's my code:
$date = "2012-04-09T08:23:07Z";
date( "F d, Y", strtotime( $date ) );
/* Result: April 09, 2012 */
My problem here is the second format Fri, 06 Apr 2012 01:25:43 0000
There is a 0000 at the last which makes the strtotime() act weird.
$date = "Fri, 06 Apr 2012 01:25:43 0000";
date( "F d, Y", strtotime( $date ) );
/* Result: April 06, 0000 */
Obviously the result should be April 06,2012.
My question here is that how can I not let strtotime() act weird when it detects a format that has a 0000 at the last. Any idea how to do this? Your help would be greatly appreciated! Thanks! :)
Thanks!
I'm pretty sure you have a formatting issue:
Fri, 06 Apr 2012 01:25:43 0000
Notice the two spaces before 0000? That's not a coincidence. The original is probably this:
Fri, 06 Apr 2012 01:25:43 +0000
Which is generated with gmdate(DATE_RFC822). In URI's, the + symbol often gets translated to a space. I think that's your problem.
How about just stripping the "0000"? Something like:
$date=str_replace("0000", "", $date);
try this:
<?php
$date = "Fri, 06 Apr 2012 01:25:43 0000";
if (strtotime($date) !== false)
{
$timestamp = strtotime($date);
echo date( "F d, Y", $timestamp );
}
else
{
echo "error";
}
?>
for $date = "Fri, 06 Apr 2012 01:25:43 0000";
outputs error.
$date = "Fri, 06 Apr 2012 01:25:43";
gives output.

Categories