Change format of date with PHP - php

I want to change this date format: "Tue Apr 3 15:00:03 GMT+0300 2012" to "3.4.2012" with PHP. Is it possible?
I tried:
$date="Tue Apr 3 15:00:03 GMT+0300 2012";
echo date('d.m.Y', strtotime($date));
but it results in: 03.04.2015. What am I doing wrong?

Read the Manual before using a function, and especially before asking other people to read it for you, like I just did to answer your question:
date('j.n.Y', strtotime($date));
Codepad Example

Your date is not formatted correctly, at least it is not the RFC 2822 date format,
Tue, 3 Apr 2012 15:00:03 +0300
strtotime is quite flexible but it cannot guess what you mean ...

Related

Why is php strtotime() not working on a date?

This is the code I have used. Why can't it understand this date?
strtotime('FRI OCT 14TH 2016'); //returns Thursday 01 01 1970
Try this:
echo date('d-m-Y', strtotime('FRI OCT 14 2016')); // remove TH from 14TH
it will return
14-10-2016
As Neat mentioned in a comment:
Because thats not a supported format, Date Formats.
Looking at the PHP documentation for Date Formats, the first row in the table describes the suffixes supported on dates (see the screenshot below). Notice that the letters are all lower-case.
.
Thus as was mentioned in comments, use lower-case letters for the suffixes.
strtotime('FRI OCT 14th 2016');
See a demonstration of this change in this playground example.

How can I convert GMT Date with php

I want to convert a date as below using php.
Mon Jun 17 2013 14:00:00 GMT 0300 (EEST)
When I run date("Y-m-d H:i:s",strtotime("Mon Jun 17 2013 14:00:00 GMT 0300 (EEST)")) the engine just generates 0300-06-17 15:56:56. If I update the code like date("Y-m-d H:i:s",strtotime("Mon Jun 17 2013 14:00:00 GMT")) it generates 2013-06-17 17:00:00. I have tried again and again but could not solve this issue. How Can I convert dates like this?
try to set the time zone and try
http://php.net/manual/en/function.date.php
Make sure you set your timezone like date_default_timezone_set('America/Los_Angeles') and try using gmdate().
http://us1.php.net/manual/en/function.date-default-timezone-set.php
http://php.net/manual/en/function.gmdate.php
If you are trying to convert GMT to your timezone then you should convert both to a UNIX timestamp and do the math.
See:
http://us3.php.net/manual/en/function.date-timestamp-get.php
Please see the solution i posted over here. You can do the similar way to convert GMT to your desired timezone.
converting datetime to gmt

Converting timestamps in php

I got time stamp in this format from twitter api.
Fri Dec 28 20:06:38 +0000 2012
I want to convert this to standard time stamp format like this one.
2012-12-10 16:20:18
Am pretty new to dates in php. How can I do it??
You can use DateTime:
$date = new DateTime('Fri Dec 28 20:06:38 +0000 2012');
echo $date->format('Y-m-d H:i:s');
The reason why I prefer DateTime is that it gives great oop implementation and makes it easier to work with dates that are quite big head-ache of programmer. For more information about this class read the manual that I have already referenced.
You can use strtotime to convert the initial string to a UNIX timestamp and then strftime to convert it back to a string:
strftime('%Y-%m-%d %H:%M:%S', strtotime('Fri Dec 28 20:06:38 +0000 2012'));
That call returns the string 2012-12-28 21:06:38 - i.e. exactly what you are looking for.
use this
$originalDate = "Fri Dec 28 20:06:38 +0000 2012";
echo $newDate = date("Y-m-d h:i:s", strtotime($originalDate));
working example http://codepad.viper-7.com/AhLcEU
In case of use php < 5.2.0 (some shared hostings) use a combination of strtotime and date
date('Y-m-d H:i:s', strtotime('Fri Dec 28 20:06:38 +0000 2012'));

strtotime fails on pubDates from RSS feeds?

Example:
print strtotime('Sat, 03 Nov 2012 20:17:12 0000');
prints nothing :(
And the date string appears to be correct..
it's:
day name, day month name year hour:min:sec timezone
Valid formats are explained in Date and Time Formats.
Consider using DateTime objects, and the createFromFormat() method
I get it you want to add a timezone correction. But the timezone requires a sign.
print strtotime('Sat, 03 Nov 2012 20:17:12 +0000');
This will work
print strtotime('03 Nov 2012 20:17:12');
Output: 1351988232
This works perfectly. You should remove the 0000 and the day (Sat)

PHP date format conversion

Need help in date conversion:
Existing date format:
Wed, 01 Dec 2010 00:00:00 -0500
(Day, DD MMM YYYY HH:MM:SS GMT-0500)
To be changed to:
2010-11-29T04:59:59-05:00
(YYYY-MM-DD(T)HH:MM:SS GMT-05:00)
How to handle in PHP?
is there any function available in php for this.
please help
strtotime() (man page) & date() (man page) or DateTime class (man page) should be able to handle this.
echo date('Y-m-d\TH:i:sZ', strtotime('Wed, 01 Dec 2010 00:00:00 -0500'));
echo date('c', strtotime('Wed, 01 Dec 2010 00:00:00 -0500')); // as mentioned by Anthony
or
echo DateTime('Wed, 01 Dec 2010 00:00:00 -0500')->format('Y-m-d\TH:i:sZ');
echo DateTime('Wed, 01 Dec 2010 00:00:00 -0500')->format('c'); // as mentioned by Anthony
First you want the date string in epoch format, so that you can use the date function. My favorite method to do this is the strtotime function:
$epoch_date = strtotime($original_date_string);
Now, you can use the date function to output it however you like. In your case, I believe you are looking for ISO 8601, which is built into the function:
$new_date_string = date('c', $epoch_date);
echo $new_date_string;
date('Y\-m\-d\Th:i:s \G\M\TP');
This will return:
2010-11-26T02:49:24 GMT-05:00
Use the date() formating its much simpler!
You can read all about it right here http://php.net/manual/en/function.date.php

Categories