Convert date to a different date format - php

All,
I have a date format in the following output that I get from Tumblr:
2013-02-25 18:00:25 GMT
I'd like to convert this to a date format I want using the date function. I tried the following:
$date = date("F d, Y",$tumblr_posts->date);
echo $date;
When I did this, the output was "January 01, 1970" which is obviously not right. Any ideas on how to fix this?
Thanks!

You need to use the strtotime function like this.
$date = date("F d, Y", strtotime($tumblr_posts->date));
To find how to parse your date use the PHP date function reference.

Related

date conversion in CodeIgniter / PHP does not gives output

I have date in this type of format: April 1st 2017 and I want to convert it into this type of format: 2017/04/01 in my CodeIgniter code using php. I have used below posted piece of code but it is not working. Please solve the issue.
Code:
$date = DateTime::createFromFormat('m/d/Y', "April 1st 2017");
echo "Date = ".$date->format('Y-m-d');
You can use strtotime() and date() php functions as
$newDate = date("m/d/Y", strtotime("April 1st 2017"));
Or in CodeIgniter
$date = DateTime::createFromFormat('j F Y - H:i', 'April 1st 2017');
echo $date->format('m/d/Y H:i:s');
Your format can be used in the constructor of DateTime. See accepted formats.
$date = new DateTime("April 1st 2017");
echo "Date = ".$date->format('Y-m-d');
Outputs:
Date = 2017-04-01
If you want to use DateTime::createFromFormat(), you have to use the proper format
"F jS Y"
The format you specified for your date is incorrect.
It would convert '04/01/2017' but it does not suit
April 1st 2017.
Try instead: createFromFormat('F dS Y')
Explanation:
F - full textual representation of a month, such as January.
d - day
S - English ordinal suffix for the day of the month
Y - 4-digit representation of year
you can try this also
<?php
$date='22 march 2018';
echo date('m/d/Y', strtotime($date));
?>

Change date format from dashes to nicer fomrat

I have dates that are being stored in the database like this:
08-11-2013
(That's Day, Month year BTW) Is there a way (without changing the database entries) to output this on a page like this instead...
8th November, 2013
Cheers.
FYI, this would be a lot easier if you stored your dates in MySQL standard Date format.
Use MySQL's DATE_FORMAT() with STR_TO_DATE()
SELECT DATE_FORMAT(STR_TO_DATE(datecol, "%d-%m-%Y"), "%D %M, %Y")
But if you have to do it with PHP you can use DateTime::createFromFormat()
$dt = DateTime::createFromFormat('08-11-2013', "d-m-Y");
echo $dt->format('jS M, Y');
While other's answers are valid, this will output the date in the exact format you asked for in your question.
$date = '08-11-2013';
$prettydate = date('jS F, Y', strtotime($date));
echo $prettydate;
In PHP try
<?php
$olddate = '2012-07-18';
$date = new DateTime($olddate);
echo $date->format('jS F, Y');
?>
Also try to read Convert date format yyyy-mm-dd => dd-mm-yyyy
Enjoy coding! :D

How to format date yy-mm-dd in PHP

I have dates currently in this format: yy-mm-dd (e.g. 2011-11-18)
I want them in this format: Friday 18 November 2011
I've tried reading through the PHP documentation manual, but I can't see how to manage dates in the format that I have. If the date needs to be in a different order I can arrange that, but I'm a bit stuck at the meoment.
Any help would be much appreciated.
Use PHP5s new date classes. Much cleaner:
$date = DateTime::createFromFormat('Y-m-d', '2011-11-18');
echo $date->format('l d F Y');
date('l j F Y', strtotime($date));
Just use starttime to change the the dates in many formats using this link.
echo date('l d F Y');
gives you the date format you want.
This was all in the manual you yourself linked.
just use strtotime to get back a timestamp and then use date() to format that:
$date = '2011-11-18'; // your date
$timestamp = strtotime($date); // convert to a timestamp
$new_date = date('l j F Y',$timestamp) // format timestamp
echo $new_date;

How to convert string to date

I would convert a string like that "July 22, 2011 in 7:42pm" in a different format.
I tried to use both strtotime and date_parse_from_format functions but they don't correctly work in this case. I suppose that the reason of this problem could be the substring "in".
How can I convert that format in a Date type or in a timestamp?
I suppose that the reason of this problem could be the substring "in".
As far as I can see you are right. At least the i is an identifier ("Minutes with leading zeros") itself, thus you must escape it.
var_dump(date_parse_from_format('F d, Y \i\n g:ia', $string));
See date() Example #2
Hey try the following solutions
print $date = "July 22, 2011 in 7:42pm";
$date = date_parse_from_format('F j, Y * h:iA', $date);
print '<pre>'; print_r($date);
I would convert a string like that "July 22, 2011 in 7:42pm" in a different format.
The static method DateTime::createFromFormat() (documentation) (or alias date_create_from_format()) is preferred. Below is an example of its use for your needs, but be sure to absorb the information found in the documentation.
How can I convert that format in a Date type or in a timestamp?
Below is a basic example. For the available formatting characters, see the date() manual page.
$subject = 'July 22, 2011 in 7:42pm';
$datetime = DateTime::createFromFormat('F d, Y \i\n g:ia', $subject);
// Output as another format (2011-07-22 19:42:00)
echo $datetime->format('Y-m-d H:i:s');
// Output as Unix timestamp (1311327720)
echo $datetime->getTimestamp();

How to reformat date in PHP?

I am pulling the dates of various posts from a database. The dates are in the following format:
2009-08-12
Numeric Year - Numeric Month - Numeric Day
How can I reformat these dates to something more user friendly like:
August 12, 2009
Numeric Month Numeric Date, Numeric Year
Assuming that the date gotten from the mysql database is stored in a variable called:
$date = $row['date_selected'];
Unlike the strtotime based examples, this allows you to ensure the month and day are interpreted in the correct order regardless of locale settings specified on the server.
$date = DateTime::createFromFormat('Y-m-d', '2009-08-12');
$output = $date->format('F j, Y');
date("F d, Y", strtotime($input))
$new_format = date("Your Date String", strtotime($date));
See:
- http://php.net/strtotime
- http://php.net/date
Basically, if strtotime() can read it correctly, you can reformat it anyway you please.
In this case, Year - Month - Day is a properly recognized strtotime() format, this might not be the case for other formats.
You might consider doing your date formatting in MySQL with your select statement:
DATE_FORMAT(date,'%M %e, %Y') as date_selected
http://www.w3schools.com/sql/func_date_format.asp
<?php
echo date('F j, Y', strtotime($date));
You might want to look at the php function strtotime:
http://php.net/manual/en/function.strtotime.php
It'll parse a large number of date representations to a Unix timestamp.
Then use the date function.
Using strtodate or explode to split the date into its different components, you can then use the date function with the appropriate format string:http://php.net/manual/en/function.date.php
$date = "2009-08-12";
list($year,$month,$day) = explode("-",$date);
$formattedDate = date("F d, Y", mktime(0,0,0,$month,$day,$year));
Outputs: "August 12, 2009"
<?php
//Date Formatter
/*
date: date you want to convert
format: its current format ie m-d-Y, m/d/Y, Y-m-d, Y/m/d
delimS: Current delimiter ie - or / or .
delimF: The delimiter you want for the result
NOTE: this will only convert m-d-Y to Y-m-d and back
*/
function dtform($date,$format,$delimS,$delimF){
$dateFinal = '';
if($format == 'm'.$delimS.'d'.$delimS.'Y'){
$dateFinal_exp = explode($delimS,$date);
$dateFinal = $dateFinal_exp[2].$delimF.$dateFinal_exp[0].$delimF.$dateFinal_exp[1];
}else if($format == 'Y'.$delimS.'m'.$delimS.'d'){
$dateFinal_exp = explode($delimS,$date);
$dateFinal = $dateFinal_exp[1].$delimF.$dateFinal_exp[2].$delimF.$dateFinal_exp[0];
}
return $dateFinal;
}
?>
Use it like this:
// February 1, 2005
print date ("F j, Y", mktime (0,0,0,14,1,2004));

Categories