I am trying to convert a date to month name and year.
$date = '2017-07-00';
$date = date('m/y', strtotime($date));
echo DATE_FORMAT($date, '%M %Y');
I am expecting output like
July, 2017
Here is error i am getting
Warning: date_format() expects parameter 1 to be DateTimeInterface, string given
No Need of DATE_FORMAT() function.
Example-1: If 00 used in day. Then, output will be June, 2017
<?php
$date = '2017-07-00';
echo date('F, Y', strtotime($date)); //June, 2017
?>
Example-2: If 01 or valid day used in day. Then, output will be July, 2017
<?php
$date = '2017-07-01';
echo date('F, Y', strtotime($date)); //July, 2017
?>
You are not using correct parameters, use F for moth and Y for year
Full code:
$date = '2017-07-00';
$date = date('F, Y ', strtotime($date));
echo $date;
Try This.
$date = '2017-07-01';
$date = date('F, Y', strtotime($date));
echo $date;
You can use this
echo date('F,Y',strtotime($date));
Related
I want to display my date as day/month/year.
example 31st October 2011
echo '<h4>Date Of Birth: '.$row['dob'].'</br></h4>';
You can try below code ::-
echo '<h4>Date Of Birth: '.date('dS F Y', strtotime($row['dob'])).'</br></h4>';
// ^^^^^^^ Here the is format of date, in which format you want.
Output will be ::-
31st October 2011
For more you can refer click here
<?php
$mydate = "2010-03-3";
$newDate = date("d M Y", strtotime($mydate));
$new_date = date('dS F Y', strtotime($newDate));
echo $new_date;
?>
/*Out Put*/
03rd March 2010
So if you have that data stored as or you can convert it to 3 variables representing DD MM YYYY then I'd suggest creating array of months and an array of post-fixes(don't know how to call it, but in your example it's 31st) and getting DD % 10 to use reminder for post-fix array
$month = ['January','February',....,'December']; //remember it's $month[MM-1];
$post = ['th','st','nd',...,'th'] //not sure if this on is correct but hope you get the idea
echo date('dS F Y', strtotime($row['dob']));
Refer to DateTime. I normally avoid strtotime() conversions.
Heres a working solution:
<?php
$date = '2016-11-12';
$newDate = new DateTime($date);
echo $newDate->format('jS F Y');
?>
OUTPUT:
12th November 2016
I've written a piece of code which converts a date to the specific format and increases it by 1 day.
<?php
date_default_timezone_set('Europe/Moscow');
$mehdate = "2011-11-25";
$mehdate = date ('d m Y', strtotime ('+1 day', strtotime($mehdate)));
echo $mehdate, "\n";
?>
But then I have to increase $mehdate by 1 day one more time.
And I cannot understand how to do that. I already tried
$mehdate = date ('d m Y', strtotime ("+1 day", $mehdate));
and
$mehdate = date ('d m Y', strtotime ('+1 day', strtotime($mehdate)));
again but it won't work because
strtotime($mehdate)
returns FALSE.
So, how can I increase the $mehdate which was already formatted?
Your issue can easily be resolved if you use DateTime class.
Try this:
$mehdate = new DateTime('2011-11-25');
$mehdate->modify('+1 day');
echo $mehdate->format('d m Y')."\n"; // Gives 26 11 2011
$mehdate->modify('+1 day');
echo $mehdate->format('d m Y'); // Gives 27 11 2011
date_default_timezone_set('Europe/Moscow');
$mehdate = "2011-11-25";
$mehdate = strtotime ('+1 day', strtotime($mehdate));
$mehdate = date ('d m Y', $mehdate);
echo $mehdate, "\n";
Result
26 11 2011
For all newbies like me there is a simple advice: don't use 'd m Y' format, you'd better operate with 'd-m-Y'.
Or you have to use DateTime class, as Object Manipulator advised.
I have a string which is in the format: dd/mm/yy
e.g. 29/03/14
But when I print it using date() I get a completely different date!
What am I missing?
$endDate = "29/03/14";
echo date("jS F, Y", strtotime( $endDate ));
1st January, 1970
I even tried:
echo date("jS F, Y", strtotime( trim($endDate) ));
With no luck!
I am reading $endDate from a text file
What I am trying to do is find out if it is the last day of the month...
i.e. Current month is 03 - March has 31 days
The day of month in file is 29
This is not the last day of the month
Your date format is not valid.
Check the Date Formats
You might consider using date_create_from_format('d/m/y','29/03/14'); and work with the DateTime object.
Use DateTime::createFromFormat
$date = DateTime::createFromFormat('d/m/y', '29/03/14');
echo $date->format("jS F, Y");
For getting the last day
echo $date->format('t');
Your date format isn't recognized by strtotime(). Use DateTime class instead:
$dateObj = DateTime::createFromFormat('d/m/y', $str);
echo $dateObj->format('jS F, Y');
Output:
29th March, 2014
Demo
What I am trying to do is find out if it is the last day of the month...
For that, you just have to check if the given date is the same as the last day of the month (which can be obtained with t format character)
$str = '29/03/14';
$dateObj = DateTime::createFromFormat('d/m/y', $str);
if ($dateObj->format('d') == $dateObj->format('t')) {
echo 'Given date is the last day of the month';
}
I have a code that print the date in this format
Sun, 09 Mar 2014 08:31:14 GMT
I want to take the Date and shorten it so I can print
the day
the month
the year
but I want to print each separately and I want to print the month in numbers
Here is the code I tried
$Date = $item->pubDate;
echo '.$Date.';
You should make use of createFromFormat of the DateTimeClass
<?php
$dt='Sun, 09 Mar 2014 08:31:14 GMT';
$date = DateTime::createFromFormat('D, d M Y G:i:s O', $dt);
echo "Day : ".$date->format('d'); // 09
echo "Month : ".$date->format('m'); //03
echo "Year : ".$date->format('Y'); //2014
Demo
Try using date function
echo date('d-m-Y', strtotime($Date));
And if you want to echo one by one
$date_to_time = strtotime($Date);
//Day
echo date('d', $date_to_time);
//Month
echo date('m', $date_to_time);
//Year
echo date('Y', $date_to_time);
Try
$date = '08/02/2014';
list($month, $day, $year) = explode('/', $date);
How I can echo 2011-01-31 as 31 January 2011?
The input (2011-01-31) comes from the database (type: date).
Also is there any option to use locale names for the month?
Edit:
$date = date_create($row['bdate']);
echo date_format($date, 'd F Y'); //31 January 2010
could be solution but date_format has no option for other languages(?). Is there any solution? Like 31 Ocak 2011
Probably is strftime what you seek.
Example:
echo strftime('%d %B %Y', strtotime($db_entry));
Use strtotime to convert 2011-01-31 to a timestamp:
echo date( 'd F Y', strtotime( $row['bdate'] ) );
<?php
echo date('d F Y', strtotime("2011-01-31"));
Demo: http://codepad.org/WPe1xCFz
Try using strftime()
$date = strtotime($row['bdate']);
echo strftime('%d %B %Y', $date);