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);
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 currently working on a package manager for my current project and I need to add package subscription date and package expiry date.
I am using php function date("Y-m-d H:i:s") i.e
$package_subscription_start_date = date("Y-m-d H:i:s");
now here is my requirement:
I need to have packages such as for 1 day, 2 days, 5 days, 1 week, 4 weeks, 12 weeks, 1 month, 6 months, 9 months, 1 year, 2 years, 5years and 10 years.
How is it possible for me to add these values and calculate the package expiry date for inserting into mysql, something like
$package_expiry_date = date("Y-m-d H:i:s") + 1 day;
$package_expiry_date = date("Y-m-d H:i:s") + 5 days;
$package_expiry_date = date("Y-m-d H:i:s") + 1 week;
$package_expiry_date = date("Y-m-d H:i:s") + 4 weeks;
$package_expiry_date = date("Y-m-d H:i:s") + 2 months;
$package_expiry_date = date("Y-m-d H:i:s") + 2 years;
Lastly I need to take care of other date issues like valid date and leap year etc.
Also I need this to be done at php level, not using mysql / sql query.
I will appreciate any help in this direction.
Something like
$date=date('Y-m-d H:i:s', strtotime('+1 day'));
$date=date('Y-m-d H:i:s', strtotime('+5 days'));
$date=date('Y-m-d H:i:s', strtotime('+1 week'));
$date=date('Y-m-d H:i:s', strtotime('+4 weeks'));
$date=date('Y-m-d H:i:s', strtotime('+2 months'));
$date=date('Y-m-d H:i:s', strtotime('+2 years'));
Have you tried the strtotime() command in php?
echo $expires = strtotime(date("Y-m-d H:i:s")." + 1 day");
echo date("Y-m-d H:i:s", $expires);
Gives you the output of
135082420020
12-10-21 08:56:40
Have a look at the php DateTime object.
Eg to add time on to a date:
//Get the current date
$date = new DateTime();
//Add 1 day
$date->modify('+1 Day');
//Output in Y-m-d format
echo $date->format('Y-m-d');
It has lots of other useful functions
<?php
echo $date = date("Y-m-d");// current da
$date1 = strtotime(date("Y-m-d", strtotime($date)) . " +1 day");
echo date("Y-m-d", $date1);
echo date("Y-m-d", $date1);
$date2 = strtotime(date("Y-m-d", strtotime($date)) . " +1 week");
echo date("Y-m-d", $date2);
$date3 = strtotime(date("Y-m-d", strtotime($date)) . " +2 week");
echo date("Y-m-d", $date3);
$date4 = strtotime(date("Y-m-d", strtotime($date)) . " +1 month");
echo date("Y-m-d", $date4);
$date5 = strtotime(date("Y-m-d", strtotime($date)) . " +1 year");
echo date("Y-m-d", $date5);
?>
I want to get the start and end date of the previous calender month.
So, it's July 2012 now, I want the function to return 01 June 2012 as start and 30 June 2012 as the end.
This is my code:
$current_month = date("Y-m-01 00:00:00");
$start_month = strtotime(date("Y-m-d", strtotime($current_month) . " -1 month"));
$end_month = strtotime(date("Y-m-d", strtotime($current_month) . " -1 second"));
echo "Start Month: " . date('Y-m-d',$start_month) . "( " . $start_month . ")<br>";
echo "End Month: " . date('Y-m-d',$end_month) . "( " . $end_month . ")<br>";
But it echo's:
Start Month: 2012-07-01( 1341093600)
End Month: 2012-07-01( 1341093600)
Any idea what I'm doing wrong?
Answering whats wrong with the code you posted, you just needed to move your round bracket over a little bit and you had it. :)
<?php
$current_month = date("Y-m-01 00:00:00");
$start_month = strtotime(date("Y-m-d", strtotime($current_month . " -1 month")));
$end_month = strtotime(date("Y-m-d", strtotime($current_month . " -1 day")));
echo "Start Month: " . date('Y-m-d',$start_month) . "( " . $start_month . ")<br>";
echo "End Month: " . date('Y-m-d',$end_month) . "( " . $end_month . ")<br>";
?>
Try this, (check the manual of strtotime)
$now = time();
$current_month = date("Y-m-01 00:00:00", $now);
$start_month = strtotime("-1 month", $now);
$end_month = strtotime("-1 second", $now);
Try something like this:
echo date("Y-m-d", mktime(0,0,0,date('m')-1,1,date('y')));
echo date("Y-m-d", mktime(0,0,0,date('m'),0,date('y')));
Seems a little bit oversized, what you (and the other answers :X) tried. It's justs
echo date('Y-m-d', strtotime('first day of last month'));
echo date('Y-m-d', strtotime('last day of last month'));
For arbitrary months
// 3 months in the past
echo date('Y-m-d', strtotime('first day of -3 months'));
echo date('Y-m-d', strtotime('last day of -3 months'));
// 3 months in the future
echo date('Y-m-d', strtotime('first day of +3 months'));
echo date('Y-m-d', strtotime('last day of +3 months'));
Read more At the manual
echo $firstdate= "01/".date('m')."/".date('Y') ;
$lastdateofmonth=date('t',date('m'));
echo $lastdate=$lastdateofmonth."/".date('m')."/".date('Y') ;
or
$date='2012-06-05 12:00:00';
echo "End = ".date("Y-m-t",strtotime($date));
echo "start = ".date("Y-m-01",strtotime($date));
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;