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.
Related
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));
?>
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 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';
}
Is there anyway to convert the value of idate("z") to a date format that reads the Day, Month, and Year? My code looks like this:
$date_int = idate("z");
$date_text = strtotime($date_int);
$date = date("l, F j, Y", $date_text);
For some reason, it's still echoing Thursday, January 1, 1970.
Any ideas?
idate("z") is incorrect as that will return the day of the year. It seems like you want idate("U"), but in that case just use date() without the second parameter, it will assume time(). Example:
$date = date("l, F j, Y");
That should be all you need.
idate("z") will only return the day of the year. Today being 74. strtotime will not understand 74 as a parsing format. Therefore date() fails all together.
Assuming you want the date of the CURRENT YEAR:
$dayOfYear = 80;
print date("l, F j, Y", strtotime($dayOfYear - idate("z") . " day")); // Wednesday, March 21, 2012
$dayOfYear = 1;
print date("l, F j, Y", strtotime($dayOfYear - idate("z") . " day")) // Monday, January 2, 2012
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;