Is there any php function available where I can add days to a date to make up another date? For example, I have a date in the following format:
27-December-2011
If I add 7 to the above, it should give:
03-January-2012.
Many thanks
Try this
$add_days = 7;
$date = date('Y-m-d',strtotime($date) + (24*3600*$add_days));
Look at this simple snippet
$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");
You can use the add method of DateTime. Anyway this solution works for php version >= 5.3
date('Y-m-d', strtotime('+6 days', strtotime($original_date)));
Actually it's easier than all that.
$some_var = date("Y-m-d",strtotime("+7 day"))
You can use a variable instead of the string, of course. It will be great if the people answering the questions, won't complicate things. Less code, means less time to waste on the server ;).
$date = new DateTime('27-December-2011');
$date->add(new DateInterval('P7D'));
echo $date->format('d-F-Y') . "\n";
Change the format string to be whatever you want. (See the documentation for date()).
$registered = $udata->user_registered;
$registered = date( "d m Y", strtotime( $registered ));
$challanexpiry = explode(' ', $registered);
$day = $challanexpiry[0];
$month = $challanexpiry[1];
$year = $challanexpiry[2];
$day = $day+10;
$bankchallanexpiry = $day . " " . $month . " " . $year;
Related
i'm developing an application in PHP and I need to use dates and the numeric representation of weekdays.
I've tried the following:
$today = date("Y-m-d");
$number = date('N', strtotime($today));
echo "Today: " . $today . " weekday: " . $number . "<br>";
$today = strtotime($today);
$tomorrow = strtotime($today);
$tomorrow = strtotime("+1 day", $today);
$number2 = date('N', strtotime($tomorrow));
echo "Tomorrow: " . date('Y-m-d', $tomorrow) . " weekday: " . $number2 . "<br>";
Output
Today: 2016-11-11 weekday: 5
Tomorrow: 2016-11-12 weekday: 4
This isn't right because the weekday of tomorrow should be 6 instead of 4.
can someone help me out?
Using DateTime would provide a simple solution
<?php
$date = new DateTime();
echo 'Today: '.$date->format( 'Y-m-d' ) .' weekday '. $date->format( 'N' )."\n";
$date->modify( '+1 days' );
echo 'Tomorrow: '.$date->format( 'Y-m-d' ) .' weekday '. $date->format( 'N' )."\n";
Output
Today: 2016-11-11 weekday 5
Tomorrow: 2016-11-12 weekday 6
However the day numbers are slightly different, the N respresents the weekday number and as you can see Friday (Today) is shown as 5. With that Monday would be 1 and Sunday would be 7.
If you look at the example below you should get the same result
echo date( 'N' );
Output
5
Date Formatting - http://php.net/manual/en/function.date.php
You have little error in the code, here`s the working one:
$today = date("Y-m-d");
$number = date('N', strtotime($today));
echo "Today: " . $today . " weekday: " . $number . "<br>";
$today = strtotime($today);
$tomorrow = strtotime($today);
$tomorrow = strtotime("+1 day", $today);
$number2 = date('N', $tomorrow);
echo "Tomorrow: " . date('Y-m-d', $tomorrow) . " weekday: " . $number2 . "<br>";
DateTime is the Object Oriented method of working with dates in PHP. I find it to be working much more fluently. That aside, it looks alot better.
// Create a new instance
$now = new DateTime();
echo $now->format('N');
// Next day
$now->modify('+1 day');
echo $now->format('N');
Resources
DateTime manual - PHP.net
You almost have it right but not quite. Why are you using strtotime on $number2? Change it to $number2 = date('N', $tomorrow); and it will work.
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'
I am feeling a bit thick today and maybe a little tired..
I am trying to add days on to a string date...
$startdate = "18/7/2011";
$enddate = date(strtotime($startdate) . " +1 day");
echo $startdate;
echo $enddate;
My heads not with it... where am i going wrong ?
Thanks
Lee
Either
$enddate = date(strtotime("+1 day", strtotime($startdate)));
or
$enddate = date(strtotime($startdate . "+1 day"));
should work. However, neither is working with the 18/7/2011 date. They work fine with 7/18/2011: http://codepad.viper-7.com/IDS0gI . Might be some localization problem.
In the first way, using the second parameter to strtotime says to add one day relative to that date. In the second way, strtotime figures everything out. But apparently only if the date is in the USA's date format, or in the other format using dashes: http://codepad.viper-7.com/SKJ49r
try this one, (tested and worked fine)
date('d-m-Y',strtotime($startdate . ' +1 day'));
date('d-m-Y',strtotime($startdate . ' +2 day'));
date('d-m-Y',strtotime($startdate . ' +3 day'));
date('d-m-Y',strtotime($startdate . ' +30 day'));
first parameter of date() is format
d.m.Y G:i:s
for example
additionally, your $startdate is invalid
You're probably looking for strtotime($startdate . "+ 1 day") or something
First you have to change the date format
by calling changeDateFormat("18/7/2011"): returns: 2011-07-18
if your parsing argument
function changeDateFormat($vdate){
$pos = strpos($vdate, '/');
if ($pos === false) return $vdate;
$pieces = explode("/", $vdate);
$thisday = str_pad($pieces[0], 2, "0", STR_PAD_LEFT);
$thismonth = str_pad($pieces[1], 2, "0", STR_PAD_LEFT);
$thisyear = $pieces[2];
$thisdate = "$thisyear-$thismonth-$thisday";
return $thisdate;
}
And this..
$startdate = changeDateFormat($startdate);
$enddate = date('Y-m-d', strtotime($startdate . "+".$noOfDays." day"));
This will work
$startdate = "18/7/2011";
$enddate = date('d/m/Y', strtotime($startdate) + strtotime("+1 day", 0));
echo $startdate;
echo $enddate;
First, the start date is parsed into integer, then the relative time is parsed.
You might also utilize the second parametr of strToTime:
$startdate = "18/7/2011";
$enddate = date('d/m/Y', strtotime("+1 day", strtotime($startdate)));
How do I get the date, one week from today, in the following format: YYYY-MM-DD ?
Try:
date("Y-m-d", strtotime("+1 week"));
This will output:
2015-12-31
If today is 2015-12-24
Just so Charles' prediction is wrong, here's a PHP 5.3+ example:
$now = new DateTime;
$interval = new DateInterval('P1W')
$next_week = $now->add($interval);
echo $next_week->format('Y-m-d');
or in slightly more compact form:
$now = new DateTime();
echo $now->add(new DateInterval('P1W'))->format('Y-m-d');
adding days, weeks, months to any date
$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");
<?php
$nextWeek = time() + (7 * 24 * 60 * 60);
echo 'Next Week: '. date('Y-m-d', $nextWeek) ."\n";
One missing from Charles prediction, straight from the horses mouth, example #1
for ($i=0 ; $i < 7 ;$i++)
{
$date[]=date('Y-m-d',strtotime("+{$i} day",time()));
}
print_r($date);
Say I have a string coming in, "2007-02-28", what's the simplest code I could write to turn that into "2007-03-01"? Right now I'm just using strtotime(), then adding 24*60*60, then using date(), but just wondering if there is a cleaner, simpler, or more clever way of doing it.
A clean way is to use strtotime()
$date = strtotime("+1 day", strtotime("2007-02-28"));
echo date("Y-m-d", $date);
Will give you the 2007-03-01
It's cleaner and simpler to add 86400. :)
The high-tech way is to do:
$date = new DateTime($input_date);
$date->modify('+1 day');
echo $date->format('Y-m-d');
but that's really only remotely worthwhile if you're doing, say, a sequence of transformations on the date, rather than just finding tomorrow.
You can do the addition right inside strtotime, e.g.
$today="2007-02-28";
$nextday=strftime("%Y-%m-%d", strtotime("$today +1 day"));
Another way is to use function mktime(). It is very useful function...
$date = "2007-02-28";
list($y,$m,$d)=explode('-',$date);
$date2 = Date("Y-m-d", mktime(0,0,0,$m,$d+1,$y));
but I think strtotime() is better in that situation...
The simplest way...
echo date('Y-m-d',strtotime("+1 day")); //from today
OR from specified date...
echo date('Y-m-d',strtotime("+1 day", strtotime('2007-02-28')));
Hello you can try this below especially if you are french
$date = date('l j F Y');
#increment the date
$date2 = date('l j F Y', strtotime("+7 day"));
to translate in french you can use the setlocale() function or the function below :
function fr_date($date){
$date = explode(' ', $date);
$date = str_replace('Monday','Lundi',$date);
$date = str_replace('Tuesday','Mardi',$date);
$date = str_replace('Wednesday','Mercredi',$date);
$date = str_replace('Thursday','Jeudi',$date);
$date = str_replace('Friday','Vendredi',$date);
$date = str_replace('Saturday','Samedi',$date);
$date = str_replace('Sunday','Dimanche',$date);
$date = str_replace('January','Janvier',$date);
$date = str_replace('February','Février',$date);
$date = str_replace('March','Mars',$date);
$date = str_replace('April','Avril',$date);
$date = str_replace('May','Mai',$date);
$date = str_replace('June','Juin',$date);
$date = str_replace('July','Juillet',$date);
$date = str_replace('August','Août',$date);
$date = str_replace('September','Septembre',$date);
$date = str_replace('October','Octobre',$date);
$date = str_replace('November','Novembre',$date);
$date = str_replace('December','Décembre',$date);
$date = implode(' ',$date);
return $date;
}
$your_date = strtotime("1month", strtotime(date("Y-m-d")));
$new_date = date("Y-m-d", $your_date++);
use strtotime() With date Formate
echo date('Y-m-d', strtotime('2007-02-28' . ' +1 day'));
$early_start_date = date2sql($_POST['early_leave_date']);
$date = new DateTime($early_start_date);
$date->modify('+1 day');
$date_a = new DateTime($early_start_date . ' ' . $_POST['start_hr'] . ':' . $_POST['start_mm']);
$date_b = new DateTime($date->format('Y-m-d') . ' ' . $_POST['end_hr'] . ':' . $_POST['end_mm']);
$interval = date_diff($date_a, $date_b);