Lets take any month be of 30 days, then for the date 25/02/2015 when we add 10 days it becomes 5/03/2015 but there are months which are of 29 and 31 days as well.If a month be of 31 then adding 10 days to 25/04/2015 would be 04/05/2015 not 05/05/2015.Does date functionality of MySQL ,PHP and Carbon function of laravel produce correct date after addition or substraction by detecting total days of particular months strictly as per calendar or just assumes every month to be of 30 days?What are the best tools for correct date manipulation?
PHP, yes.
<?php
$date=date_create("2015-02-25");
date_add($date, date_interval_create_from_date_string("10 days"));
echo date_format($date,"Y-m-d");
// echos 2015-03-07
date_add($date, date_interval_create_from_date_string("-10 days"));
echo date_format($date,"Y-m-d");
// echos 2015-02-25
MySQL, yes.
DATE_ADD(SomeDate, INTERVAL 10 DAY)
DATE_SUB(SomeDate, INTERVAL 10 DAY)
You can use like this to get current date,previous date and next date
<?php
$date = date('Y-m-d');
$prev_date = date('Y-m-d', strtotime($date .' -1 day'));
$next_date = date('Y-m-d', strtotime($date .' +1 day'));?>
Related
I have the following code which gets the date parsed, the subtracts 1 month. This works perfectly.
$date = '22-05-2016';
print(date("Y-m-d 23:59:59", strtotime($date.' -1 months')));
// outputs 2016-04-22 23:59:59
There are times where I need to force the date to month end. For this I use Y-m-t instead of Y-m-d Which works perfectly.
$date = '22-05-2016';
print(date("Y-m-t 23:59:59", strtotime($date.' -1 months')));
// outputs 2016-04-30 23:59:59
The problem comes when the date that is parsed is actually the last day of that month. It then flips to end of the next month.
$date = '31-05-2016';
print(date("Y-m-t 23:59:59", strtotime($date.' -1 months')));
Actual Output 2016-05-31 23:59:59 (hasn't removed 1 month)
Desired Output 2016-04-30 23:59:59
EDIT: Fiddle example http://ideone.com/0fqlor
Try:
$date = '31-05-2016';
print(date("Y-m-d 23:59:59", strtotime($date.' last day of last month')));
and dig into strtotime possibilities ;)
Take a look here: http://php.net/manual/en/datetime.formats.relative.php
Last Note: -1 month only gets 30 days back in time. Therefore it will not work allways. Some kind of PHP stuff, i think ;)
strtotime is your friend. The last day of any "given" month can be retrieved using the verbal String last day of May 2015 - so, for easy input (guess you have a date-picker not a month-picker), convert the selected date to its month expression first:
date_default_timezone_set('Europe/London');
$to = '31-05-2016';
$month = date("M Y", strtotime($to));
$strtotime_expression = "last day of " . $month;
echo $strtotime_expression.": <br />";
print(date("Y-m-t 23:59:59", strtotime($strtotime_expression)));
http://ideone.com/YvjVeP
I am working on date and I am stuck at a point. How can I get the date after 1.2 or after 1.5 year from the given date?
My code is as follows:
$date = date("Y-m-d", strtotime($from_date . ' +'.$valid_duration.' '.$day) );
where $valid duration can be number as 1, 2, 1.2, etc. and $day is year, months, days.
To get the future date try this:
$StartingDate = date('Y-m-d'); // todays date as a timestamp
$newEndingDate = date("Y-m-d", strtotime(date("Y-m-d", strtotime($StartingDate)) . " + 1 year 2 months 5 days"));
Hope this helps.
Not sure if you can define years/months/days with a decimal point.
But regardless, have you considered just using the timestamp?
86400 seconds in a day, so say you wanted to get date from one year in the future, you could use something like:
$thetime = time() + (365 * 86400);
$date = date("Y-m-d", $thetime);
Hy, I have in database number of the week date ("W") and I want to display week interval like 28 Jan -> 3 Feb in this format, and I don't know if it's possible. Can you help?
Thanks!
Try this
$year = 2013;
$week_no = 6;
$week_start = new DateTime();
$week_start->setISODate($year,$week_no);
$week_end = clone $week_start;
$week_end = $week_end->add(new DateInterval("P1W"));
echo $week_start->format('d-M-Y') . " - ".$week_end->format('d-M-Y');
Transform your intervals into timestamps.
If its not the first day of the week get the first day of that week with strtotime "last sunday" (or monday) for the first date.
Do the same for the second date this time geting the last day of the week with "next saturday" (or sunday);
Get both dates W and make a mysql comparison between the weeks.
I expected this functional to return 6/30/2005 instead of 7/1/2005.
print date("m/d/Y", strtotime("12/31/2004 +6 month"));
Similarly, print date("m/d/Y", strtotime("1/31/2011 +1 month")) returns 03/03/2011 while would like it to return 2/28/2011.
Does anyone know if there is a straight forward way to show the last day of the added month?
How about this?
echo date("m/d/Y", strtotime("last day of 12/31/2004 + 6 month")); // 6/30/2005
echo date("m/d/Y", strtotime("last day of 1/31/2011 + 1 month")); // 2/28/2011
Demo
Edit: For your reference, here is a link to the documentation for relative times.
as strtotime continue in to next month if there isn't enoghe days that month,
you can back 6 month and check if its end up on the start date
$date2 = date("Y-m-d", strtotime("{$date} +6 months"));
$date3 = date("Y-m-d", strtotime("{$date2} -6 months"));
if($date3 != $date)
{
$date2 = date("Y-m-t", strtotime("{$date2} -1 months"));
}
(or in your case "m/t/Y")
One simple way is to actually go one month ahead of the day you want and then make the day value zero. Also, mktime() might be easier
$mymonth = 2; // I want the last day of February
echo date('m/d/Y', mktime(0,0,0,$mymonth+1,0,2011));
This should return 2/28/2011.
strtotime does the best it can with conflicting information. Saying
1/31/2011 +1month
would mean advancing to
2/31/2011
but February only has 28 (sometimes 29) days. 2011 isn't a leap year, so the "31st of February" gets normalized to "March 3rd".
The same applies for '12/31/2004 +6month'. That takes you to June 31st, 2005. But June only has 30 days, so the date is normalized to July 1st instead.
I have a fixed date from MySql
startDate = 07/03/2011
I wanted to add 60 days on top this date to have an endDate.
$startDate = $result['startDate'];
$endDate = ??? + strtotime("+60 days");
echo $endDate;
From my research, I know it has something do with strtotime, but all the sites I come across with based the start date from current workstation's time. My date is already fixed and entered prior to running and getting the endDate.
Help? Thanks in advance!
In addition to PHP solutions others are providing, you can create the endDate right inside of MySQL and save yourself some of the trouble:
SELECT startDate, DATE_ADD(startDate, INTERVAL 60 DAY) AS endDate FROM table;
-- Or by months (not exactly the same thing)
SELECT startDate, DATE_ADD(startDate, INTERVAL 2 MONTH) AS endDate FROM table;
Relevant documentation here...
You could reformat the results of strtotime()
$startDate = $result['startDate']; // 07/03/2011
$endDate = date("m/d/Y", strtotime("$startDate +60 days"));
Demo: http://codepad.org/9rWnoeQb
$startDate = "07/03/2011";
$endDate = strtotime("+60 days",time($startDate));
$formatted = date('m/d/Y',$endDate);
echo $endDate . "<br/>" . $formatted;
86400 seconds in a day, times number of days.. and add it to current time.
$nextMonth = time()+86400*60;
echo date("Y-m-d H:i:s", $nextMonth);