Anyone knows how to calculate date based on number of week/Month/Year?
suppose if i set the period as 2 weeks, I should get the date 2 week after the current date. If the period is 3 weeks, then, I should get 3 week after current date. Similarly for month and year as well. Can anybody please help with reference code? I am not able to implement this.
Is there any predefined function for this?
I'm not quite clear on what you're trying to get, but take a look at this answer at joomla.stackexchange.com to see the PHP-esque date calculations that are possible. It's about quarters, but with variations like strtotime uses, you should be able to get what you need.
$date = date("Y-m-d");// current date
$date = strtotime(date("Y-m-d", strtotime($date)) . " +1 day");
$date = strtotime(date("Y-m-d", strtotime($date)) . " +1 week");
$date = strtotime(date("Y-m-d", strtotime($date)) . " +2 week");
$date = strtotime(date("Y-m-d", strtotime($date)) . " +1 month");
$date = strtotime(date("Y-m-d", strtotime($date)) . " +30 days");
Related
This question already has answers here:
increment date by one month
(21 answers)
Closed 4 years ago.
I want to increase date by one month but,
current date is working , but my need is to increase date by one month ( dynamically )
I tried this from response bellow
$regdate=$row2['created_date'];
$onemonth = date($regdate, strtotime("+1 month"));
How to add $regdate variable to date function...?
Try this
$regdate=$row2['created_date'];
$date = new DateTime($regdate);
$interval = new DateInterval('P1M');
$date->add($interval);
$onemonth = $date->format('Y-m-d');
in my point of view , i think it will be perfect if you got the data edited from database before the php statements :
MYSQL level :
SELECT DATE_ADD( yourDate, INTERVAL 1 month ) from your table .
Try this
$regdate = strtotime($row2['created_date']);
echo date('Y-m-d',strtotime("+1 month",$regdate));
try the following:
added 1 month in given date
$date = $row2['created_date'];
$date = date("Y-m-d", strtotime(date("Y-m-d", strtotime($date)) . " +1 month") );
echo $date;
You can add the required time period as below:
Add day
$date = date("Y-m-d", strtotime(date("Y-m-d", strtotime($date)) . " +1 day") );
Multiple days
$date = date("Y-m-d", strtotime(date("Y-m-d", strtotime($date)) . " +15 days") );
Add week
$date = date("Y-m-d", strtotime(date("Y-m-d", strtotime($date)) . " +1 week") );
Add month
$date = date("Y-m-d", strtotime(date("Y-m-d", strtotime($date)) . " +1 month") );
I know that date("Y"), date("m") and date("d") will return current Year (2013), Month (07) and Date (11) respectively.
I am working with date Format: "2013-07-11". I have current date like this. Now I want to get the value "2013-06-11" and "2013-08-11" somehow using PHP.
What might be the code to get this values (Last Month's Same Date, and Next Month's Same Date)?
I tried:
$LastMonth = date ("m") - 1;
$LastDate = date("Y") . "-0" . $LastMonth . "-" . date("d");
But this will return error when it is October. In October it will show "2013-010-11".
What can be a better solution? Can anyone help me?
Use it with PHP's strtotime():
echo date('Y-m-d', strtotime('+1 month')); //outputs 2013-08-11
echo date('Y-m-d', strtotime('-1 month')); //outputs 2013-06-11
$date = new DateTime( "2013-07-11");
$date->modify("+1 month");
echo $date->format(‘l, F jS, Y’);
Try this
$lst_month=mktime(0,0,0,date('m')-1,date('d'),date('Y'));
echo "M<br>". date("Y-m-d",$lst_month);
$next_month=mktime(0,0,0,date('m')+1,date('d'),date('Y'));
echo "M<br>". date("Y-m-d",$next_month);
$nextmonth=date("dmy",strtotime("+1 month"));
$lastmonth=date("dmy",strtotime("-1 month"));
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
adding one day to a date
I'm trying to add a day to a value pulled from a mysql row.
so the value getting returned is let's say
2012-10-22 22:12:13
and I want to make it
2012-11-22 22:12:13
and store it in the variable without having to interval it back into mysql and then pull it right back out.
i tried doing
$end_date_add = $enddate + 0000 . "-" . 00 . "-" . 01 . " " . 00 . ":" . 00 . ":" . 00;
with $end_date being the time logged, but it replaces the time with zeros.
am I going about this wrong?
Any help much appreciated, thank you.
This is what you want, i guess...
$date_old = strtotime("+1 MONTH", strtotime("2012-10-22 22:12:13"));
echo date("Y-m-d H:i:s", $date_old);
You can make use of strtotime to add the one month period:
$date = '2012-10-22 22:12:13';
$format = 'Y-m-d H:i:s';
echo date($format, strtotime("$date +1 MONTH"));
Output (Demo):
2012-11-22 22:12:13
You can also make use of PHP's DateTime type and add a DateInterval of one day:
$date = '2012-10-22 22:12:13';
$format = 'Y-m-d H:i:s';
echo (new DateTime($date))->add(new DateInterval('P1M'))->format($format);
Output (Demo):
2012-11-22 22:12:13
The code above is PHP 5.4, in PHP 5.3 you can do:
echo date_add(new DateTime($date), new DateInterval('P1M'))->format($format);
Date adding
$date = date("Y-m-d"); // current date
$date = strtotime(date("Y-m-d", strtotime($date)) . " +1 day");
$date = strtotime(date("Y-m-d", strtotime($date)) . " +1 week");
$date = strtotime(date("Y-m-d", strtotime($date)) . " +2 week");
$date = strtotime(date("Y-m-d", strtotime($date)) . " +1 month");
$date = strtotime(date("Y-m-d", strtotime($date)) . " +30 days");
This could also be done as part of the MYSQL query
SELECT DATE_ADD(datecol, INTERVAL 1 DAY) AS 'datecol_plus_one'
How can I find the next day's date after a given date.
For example, I have a date 2011-11-31. How can I get the next date (2011-12-01) using PHP?
Similarly, how would I get the previous day's date?
$nextdate = date("Y-m-d",strtotime($olddate." +1 day"));
You can use the strtotime() function in php:
$date="2011-11-11";
$nextdate = date('Y-m-d', strtotime($date . " +1 day"));
echo $nextdate;
It will echo as "2011-11-12".
Similarly to find the previous date use:
$nextdate = date('Y-m-d', strtotime($date . " -1 day"));
It will echo as "2011-11-10".
In my PHP code I have a date in my variable "$postedDate".
Now I want to get the date after 7 days, 15 days, one month and 2 months have elapsed.
Which date function should I use?
Output date format should be in US format.
Use strtotime.
$newDate = strtotime('+15 days',$date)
$newDate will now be 15 days after $date. $date is unix time.
http://uk.php.net/strtotime
try this
$date = date("Y-m-d");// current date
$date = strtotime(date("Y-m-d", strtotime($date)) . " +1 day");
$date = strtotime(date("Y-m-d", strtotime($date)) . " +1 week");
$date = strtotime(date("Y-m-d", strtotime($date)) . " +2 week");
$date = strtotime(date("Y-m-d", strtotime($date)) . " +1 month");
$date = strtotime(date("Y-m-d", strtotime($date)) . " +30 days");
Since PHP 5.2.0 the DateTime build in class is available
$date = new DateTime($postedDate);
$date->modify('+1 day');
echo $date->format('Y-m-d');
http://php.net/manual/en/class.datetime.php
$date=strtotime(date('Y-m-d')); // if today :2013-05-23
$newDate = date('Y-m-d',strtotime('+15 days',$date));
echo $newDate; //after15 days :2013-06-07
$newDate = date('Y-m-d',strtotime('+1 month',$date));
echo $newDate; // after 1 month :2013-06-23
This is very simple; try this:
$date = "2013-06-12"; // date you want to upgade
echo $date = date("Y-m-d", strtotime($date ." +1 day") );
What’s the input format anyway?
1) If your date is, say, array of year, month and day, then you can mktime (0, 0, 0, $month, $day + 15, $year) or mktime (0, 0, 0, $month + 1, $day, $year). Note that mktime is a smart function, that will handle out-of-bounds values properly, so mktime (0, 0, 0, 13, 33, 2008) (which is month 13, day 33 of 2008) will return timestamp for February, 2, 2009.
2) If your date is a timestamp, then you just add, like, 15*SECONDS_IN_A_DAY, and then output that with date (/* any format */, $postedDate). If you need to add one month 30 days won’t of course always work right, so you can first convert timestamp to month, day and year (with date () function) and then use (1).
3) If your date is a string, you first parse it, for example, with strtotime (), then do whatevee you like.