PHP Date Format Issue - php

I have date in following format (Y-m-d) as 2010-11-24, need to convert it to
1) 24 Nov, 2010
2) 24_Nov_2010
Help appreciated, thanks.

$oDate = new DateTime('2010-11-24');
echo $oDate->format('d M, Y') . "\n";
echo $oDate->format('d_M_Y') . "\n";

(d M, Y)
(d_M_Y)
So, something like
date("d M, Y");
Will output todays date:
24 Nov, 2010
Edit: I may have read your question incorrectly. Can you please clarify if this is what you want? Or you wanted to convert the 24-11-2010 into the above formats?
If the latter if what you were seeking, you can simply utilise the strtotime function:
date("d M, Y", strtotime("2010-11-24"));

Most simple, you can use strtotime() to convert 2010-11-24 into date, and use date() to get seperate pieces(Y,m,D) and convert them to whatever format you want.
Please refer this link:
http://php.net/manual/en/function.strtotime.php
http://php.net/manual/en/function.date.php

$d1 = "2010-11-24";
echo date("d M, Y", strtotime($d1)); // 24 Nov, 2010
echo date("d_M_Y", strtotime($d1)); // 24_Nov_2010

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

Display todays day of the week, month, day, year in THIS format in PHP?

I would like to display this format of the current date using PHP. I have googled and found a few variances of what I want to do but the company I work for wants it specifically in this format to match what they've mailed out.
Monday, March 16, 2015
Thanks.
If you looked at the PHP documentation for date you'd be able to figure it out very easily:
echo date('l, F j, Y');
which outputs
Monday, March 16, 2015
As mentioned by #alfallouji, you should have a look at the php documentation.
Here is what you need:
$now = new \DateTime();
echo $now->format('l, F j, Y');
The date format string should be "l, F d, Y".
You can display current date in this format as below:
<?php
echo date("l, F d, Y");
?>

PHP Date Formatting from MySQL Fatched Datetime [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 8 years ago.
Can anyone please help?
I have searched many website (including stackoverflow) but can't find my exact answer.
It may be so easy, but I need to know.
2014-11-21 18:49:55
I need to convert it to
6:49 PM, 21th November, 2014
Can anyone help me please?
Thanks in advance for helping.
This should work for you:
$date = "2014-11-21 18:49:55";
echo date('g:i A, dS F Y', strtotime($date));
Output:
6:49 PM, 21st November 2014
Try this-
$date = "2014-11-21 18:49:55";
echo date('g:i A, jS F, Y', strtotime($date));
Output -
6:49 PM, 21st November, 2014
$originalDate = "2014-11-21 18:49:55";
$newDate1 = date("g:i A,", strtotime($originalDate));
$newDate2 = date("d", strtotime($originalDate));
$newDate3 = date("F, Y", strtotime($originalDate));
echo $newDate1.' '.$newDate2.'th '.$newDate3;
<?php
$providedTime = '2014-11-21 18:49:55';
$newTime = strtotime($providedTime);
echo date('g:i A, dS F Y', $newTime);
?>
[Working example][1]
We can use PHP's date() function to get the provided date in required format.
the date() function has various parameters and we can use nice use of them.
Hope it works for you.

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;

PHP date conversion

I have date in format 2011-01-28 06:34:33 i.e. date("Y-m-d H:i:s"). I want to convert it into 28th January 2011.
How can I change it?
Supply the date function with your format, which can be found here. Pass the timestamp of your original date as the second parameter to date. You can obtain the timestamp by using strtotime.
date("dS F Y", strtotime("2011-01-28 06:34:33"));
Use
$dateStr = date("jS F Y", time());
The day value is without leading zero.
Try this echo date('jS F Y h:i:s A');

Categories