last year, this year, next year with php DateTime - php

I am trying to create a dropbox that will display the last year, the current year and the next year using the php DateTime object.
In my current code I create three objects and have to call a method on 2 of them. This seems a bit heavy on the resources.
$today = new DateTime();
$last_year=new DateTime();
$last_year->sub(new DateInterval('P1Y'));
$next_year = new DateTime();
$next_year->add(new DateInterval('P1Y'));
echo date_format($last_year, 'Y').' '.date_format($today, 'Y').' '.date_format($next_year, 'Y');
another way I found to only use 1 object is
$today = new DateTime();
echo date_format($today->sub(new DateInterval('P1Y')), 'Y').' '.date_format($today->add(new DateInterval('P1Y')), 'Y').' '.date_format($today->add(new DateInterval('P1Y')), 'Y');
but that will become very confusing.
Can someone tell me a better way to do this using DateTime()? As I will need something similar for months ?

Depending upon your version of PHP (>= 5.4), you could tidy it up a bit like this:-
$today = new DateTime();
$last_year=(new DateTime())->sub(new DateInterval('P1Y'));
$next_year = (new DateTime())->add(new DateInterval('P1Y'));
echo $last_year->format('Y').' '.$today->format('Y').' '.$next_year->format('Y');
See it working.
A more readable and concise option may be to use \DateTimeImmutable.
$today = new DateTimeImmutable();
$one_year = new DateInterval('P1Y');
$last_year = $today->sub($one_year);
$next_year = $today->add($one_year);
echo $last_year->format('Y').' '.$today->format('Y').' '.$next_year->format('Y');
See it working.
Other than that, this all looks fine. Worry about optimisation when it is needed.

Try this. This quite efficient.
echo date("Y");
echo date("Y",strtotime("-1 year"));
echo date("Y",strtotime("+1 year"))

May be you can also limit the call of new DateInterval('P1Y') by creating one object and using it for all three calculations?
$interval = new DateInterval('P1Y');
$dateTime = new DateTime();
$lastYear = $dateTime->sub($interval)->format('Y');
$dateTime = new DateTime();
$nextYear = $dateTime->add($interval)->format('Y');
$dateTime = new DateTime();
$thisYear = $dateTime->format('Y');
echo $lastYear . ' ' . $thisYear . ' ' . $nextYear;
and by breaking the single string into multiple commands always helps me in reducing confusions.

<?php
$d = new DateTime('now');
$cy = $d->format('Y');
// Get previous year
$d->modify('-1 year');
$py = $d->format('Y');
//Next year : Since object has previous year, so +2 to get next year
$d->modify('+2 year');
$ny = $d->format('Y');
echo "Previous Year: ".$py."<br>";
echo "Current Year : ".$cy."<br>";
echo "Next Year : ".$ny."<br>";
$d = new DateTime('now');
$cm = $d->format('m');
$d->modify('-1 month');
$pm = $d->format('m');
$d->modify('+2 month');
$nm = $d->format('m');
echo "Previous Month: ".$pm."<br>";
echo "Current Month : ".$cm."<br>";
echo "Next Month : ".$nm."<br>";
?>
Output
Previous Year: 2013
Current Year : 2014
Next Year : 2015
Previous Month: 12
Current Month : 01
Next Month : 02

Just use basic math:
$current = date('Y');
$prev = $current - 1;
$next = $current + 1;

after reading all the answers I came up with this function to create a dropdown box for year.
$name is the name of the
$year_number is the number of years you want to display
this function starts with one year before the current year but you can easily modify it to start displaying from earlier years.
function drop_down_box_date_year(&$dbconn, $name, $year_number){
$today = new DateTime();
$interval = new DateInterval('P1Y');
$startyear = (new DateTime())->sub($interval);
echo '<select name="'.$name.'_year">';
for($i=0;$i<$year_number;$i++){
if($startyear->format('Y')==$today->format('Y')){
echo '<option value="'.$startyear->format('Y').'" selected>'.$startyear->format('Y').'</option>';
}else{
echo '<option value="'.$startyear->format('Y').'">'.$startyear->format('Y').'</option>';
}
$startyear->add($interval);
}
echo'</select>';
}

Related

Adding 12 months to February 29

I want to add 12 months to my date. My start date is 02/29/2020 and I want to add 12 months to this.
Code:
$startdate = '02/29/2020';
date('m/d/Y', strtotime('+12 months', strtotime($startdate)));
Output:
03/01/2021
I used this code to add 12 months but the output is 03/01/2021, when the real output should be 02/28/2020.
Have a look!
function add_months($months, DateTime $dateObject)
{
$next = new DateTime($dateObject->format('Y-m-d'));
$next->modify('last day of +'.$months.' month');
if($dateObject->format('d') > $next->format('d')) {
return $dateObject->diff($next);
} else {
return new DateInterval('P'.$months.'M');
}
}
function getCalculatedDate($d1, $months)
{
$date = new DateTime($d1);
// call second function to add the months
$newDate = $date->add(add_months($months, $date));
//formats final date to m/d/Y form
$dateReturned = $newDate->format('m/d/Y');
return $dateReturned;
}
An example would be:-
$startDate = '02/29/2020';
$nMonths = 12; // choose how many months you want to add
$finalDate = getCalculatedDate($startDate, $nMonths); // output: 02/28/2021
This way you will get the output of 02/28/2021
$startdate = '02/29/2020';
$date = date('m/d/Y', strtotime($startdate . '+365 days'));
using DateTime and DateInterval objects leads to 03/01/2021
$date = new \DateTime('02/29/2020');
$date->add(new \DateInterval('P12M'));
echo $date->format('m/d/Y');
for me 03/01/2021 is not always a bad answer

How to calculate date in PHP using variables

Can anyone help create this logic? Kind of difficult to explain...
Looking to take a date, add 3 days then select the next date based on a database value.
Say we start with:
$end_date = "2017-08-23 23:59:59"
$payday = 5; //1=monday, 2=tuesday, 3=wednesday, 4=thursday, 5=friday
//And we want to calculate $paydate:
$temp_date = $end_date + 3 days;
$pay_date = the first $payday(day of week) after $temp_date
Any ideas how to write this in php? This one is stumping me. Thanks!
To add three days you can do this:
$date = new DateTime('2017-08-23 23:59:59');
$date->add(new DateInterval('P3D'));
$date->modify("next friday");
echo $date->format('Y-m-d') . "\n";
You could also use a lookup table, or array, that matches number to named days of the week and use something like $date->modify("next $days[$payday]");
where
$days = [ [1] => "monday",
.... etc
How about this? It will get a date, add 3 days, and then loop through the next days until it finds a day in the $days array (which you can get from a database):
<?php
$date = new DateTime('2017-08-23 23:59:59');
function nextPayday($date) {
$date->add(new DateInterval('P3D'));
echo "Date + 3 days: ".$date->format('D Y-m-d') . "<br>";
$payDate = $date->add(new DateInterval('P1D'));
$days = ["1", "2", "3", "4", "5"];
while (!in_array($payDate->format("N"), $days)) {
$payDate->add(new DateInterval('P1D'));
}
return $payDate->format("D Y-m-d");
}
echo "Date + 3 days: ".$date->format('D Y-m-d') . "<br>";
echo "Next payday: ".nextPayday($date);
Demo
Or, if you need to find a next specific day, use this function instead:
function nextPayday($date) {
$date->add(new DateInterval('P3D'));
echo "Date + 3 days: ".$date->format('D Y-m-d') . "<br>";
$payDate = $date->add(new DateInterval('P1D'));
$day = "5";
while ($payDate->format("N") !== $day) {
$payDate->add(new DateInterval('P1D'));
}
return $payDate->format("D Y-m-d");
}
Jeff, take a look at Carbon which extends PHP's DateTime object.
It allows you to deal with dates in a very clean and intuitive way. Based on your example:
$date = Carbon::parse('next monday')->toDateString();
Adjust that to your exact case.
Another version to achieve the objective:
function nextPayday($dateString, $paydayNum) {
$paydays = [
1=>'monday',
2=>'tuesday',
3=>'wednesday',
4=>'thursday',
5=>'friday'
];
$temp_date_stamp = date('d-m-Y H:i:s', strtotime($dateString.' +3 days'));
$pay_date_stamp = strtotime('first '.$paydays[$paydayNum].' '.$temp_date_stamp);
return date('d-m-Y H:i:s', $pay_date_stamp);
}
$end_date = "2017-08-23 23:59:59";
$payday = 5;
echo nextPayday($end_date, $payday);
result:
01-09-2017 23:59:59

Compare Day with day and find Next date of Day in PHP

I'm working with dates in php and I'm a little stumped
This is my code:
$day = 'Thursday';
$i = 0;
$o_date = new DateTime("2012-09-12 20:56:43 +18 hours");
$date = date_format($o_date, 'l');
$full = date_format($o_date, 'd-m-Y');
if($day!=$date) {
$date = new DateTime($date . " +1 days");
$i++;
}
$order_day = new DateTime($full . " +".$i." days");$order_day = date_format($order_day, 'D, d M');
return $order_day;
This is what I want it to do:
1) I've got a day in string format (eg. Thursday)
2) I've got an order date (eg. 2012-09-12 20:56:43)
I want to get that date and turn the day into a string (eg date_format($o_date, 'l'); so that will return Wednesday, I then want to count how many days until the next Thursday after the order date, which I've built an if loop for. I then want to get the original date and add the amount of days the $i has accumulated, then return the date in day format (eg. Thursday, 13 September, but for some reason my code isn't working. Can someone see what I've done wrong here?
Instead of a loop, why don't you use the numeric day and calculate:
$day = 'Thursday';
$day_names = array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');
$day_num = array_search($day, $day_names);
$o_date = new DateTime("2012-09-12 20:56:43 +18 hours");
$o_day_num = $o_date->format('w');
$day_diff = ($day_num - $o_day_num) % 7;
if ($day_diff == 0) { $day_diff = 7; }
$order_day = clone $o_date;
$order_day->add(new DateInterval("P".$day_diff."D"));
So I actually like #Barmar's idea better, but this is what I came up with... there were a number of issues in your original code, including formatting of the initial date, and the fact that you are creating a new object each time instead of working with the same one.
If you choose to use a loop, and I'm not sure you should... try this:
<?php
$day = 'Thursday';
$i = 0;
// Order Date
$o_date = new DateTime("2012-09-12 20:56:43 +18");
$date = $o_date->format('l');
while($day!==$date) {
$date = $o_date->add(new DateInterval('P1D'))->format('l');
$i++;
}
$order_day = $o_date->format('D, d M');

plus Week with PHP Date with GET?

I have URL:
http://www.mysite.com/
here is:
$date = new DateTime("2011-11-28");
this show me:
2011-11-28
i would like add for this site buttons PREVIOUS and NEXT.
If i click NEXT then i have url:
http://www.mysite.com/number/2
now this show me:
2011-12-05 //(+ 7 days)
and i have buttons PREVIOUS (http://www.mysite.com/number/1) and NEXT (http://www.mysite.com/next/3)
etc.
How can i modify
$date = new DateTime("2011-11-28");
with $_GET['number'] ? i have modify from 2011-11-28 interval 7 day!
Check out DateTime::modify (available in PHP >= 5.2)
What you want to do is:
// just for checking if nobody is tampering with the number variable:
if (strcspn($_GET['number'], '0123456789') != strlen($_GET['number']) {
die('Invalid input!');
}
// actual calculation:
$amountOfDays = ((int)$_GET['number']) * 7;
$date = new DateTime("2011-11-28");
$date->modify('+'.$amountOfDays.' day');
Alternatively, you can use DateTime::add, but that is only defined in PHP versions >= 5.3.
Use add and sub to add/subtract days.
$date = new DateTime("2011-11-28");
$num = $_GET['number'];
$interval = new DateInterval('P'.$num.'W');
$date->add($interval);
Or, you can use modify
$date = new DateTime("2011-11-28");
$num = $_GET['number'];
$date->modify(($num>0 ? '+' : '').$num.' weeks');
You can also have a look at mktime()
http://php.net/manual/en/function.mktime.php
You can convert the primary date to a timestamp and add 7 days:
$new_date = mktime(0, 0, 0, 11, (28 + intval($_GET['number'])), 2011);
$visible_date = date('Y-m-d', $new_date);
echo $visible_date;
Use:
$date = new DateTime('#'.strtotime( $_GET['number'] . ' week'));
where $_GET['number'] is a natural number.
Try this:
$days = (intval($_GET['number']) - 1) * 7;
$date = new DateTime('2000-01-01');
$date->add(new DateInterval("P$daysD"));
I like strtotime a lot :)
It works like this:
$date = new DateTime("2011-11-28");
$prevWeek = strtotime('-1 week',$date);
$nextWeek = strtotime('+1 week',$date);

Next business day of given date in PHP

Does anyone have a PHP snippet to calculate the next business day for a given date?
How does, for example, YYYY-MM-DD need to be converted to find out the next business day?
Example:
For 03.04.2011 (DD-MM-YYYY) the next business day is 04.04.2011.
For 08.04.2011 the next business day is 11.04.2011.
This is the variable containing the date I need to know the next business day for
$cubeTime['time'];
Variable contains: 2011-04-01
result of the snippet should be: 2011-04-04
Next Weekday
This finds the next weekday from a specific date (not including Saturday or Sunday):
echo date('Y-m-d', strtotime('2011-04-05 +1 Weekday'));
You could also do it with a date variable of course:
$myDate = '2011-04-05';
echo date('Y-m-d', strtotime($myDate . ' +1 Weekday'));
UPDATE: Or, if you have access to PHP's DateTime class (very likely):
$date = new DateTime('2018-01-27');
$date->modify('+7 weekday');
echo $date->format('Y-m-d');
Want to Skip Holidays?:
Although the original poster mentioned "I don't need to consider holidays", if you DO happen to want to ignore holidays, just remember - "Holidays" is just an array of whatever dates you don't want to include and differs by country, region, company, person...etc.
Simply put the above code into a function that excludes/loops past the dates you don't want included. Something like this:
$tmpDate = '2015-06-22';
$holidays = ['2015-07-04', '2015-10-31', '2015-12-25'];
$i = 1;
$nextBusinessDay = date('Y-m-d', strtotime($tmpDate . ' +' . $i . ' Weekday'));
while (in_array($nextBusinessDay, $holidays)) {
$i++;
$nextBusinessDay = date('Y-m-d', strtotime($tmpDate . ' +' . $i . ' Weekday'));
}
I'm sure the above code can be simplified or shortened if you want. I tried to write it in an easy-to-understand way.
For UK holidays you can use
https://www.gov.uk/bank-holidays#england-and-wales
The ICS format data is easy to parse. My suggestion is...
# $date must be in YYYY-MM-DD format
# You can pass in either an array of holidays in YYYYMMDD format
# OR a URL for a .ics file containing holidays
# this defaults to the UK government holiday data for England and Wales
function addBusinessDays($date,$numDays=1,$holidays='') {
if ($holidays==='') $holidays = 'https://www.gov.uk/bank-holidays/england-and-wales.ics';
if (!is_array($holidays)) {
$ch = curl_init($holidays);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
$ics = curl_exec($ch);
curl_close($ch);
$ics = explode("\n",$ics);
$ics = preg_grep('/^DTSTART;/',$ics);
$holidays = preg_replace('/^DTSTART;VALUE=DATE:(\\d{4})(\\d{2})(\\d{2}).*/s','$1-$2-$3',$ics);
}
$addDay = 0;
while ($numDays--) {
while (true) {
$addDay++;
$newDate = date('Y-m-d', strtotime("$date +$addDay Days"));
$newDayOfWeek = date('w', strtotime($newDate));
if ( $newDayOfWeek>0 && $newDayOfWeek<6 && !in_array($newDate,$holidays)) break;
}
}
return $newDate;
}
function next_business_day($date) {
$add_day = 0;
do {
$add_day++;
$new_date = date('Y-m-d', strtotime("$date +$add_day Days"));
$new_day_of_week = date('w', strtotime($new_date));
} while($new_day_of_week == 6 || $new_day_of_week == 0);
return $new_date;
}
This function should ignore weekends (6 = Saturday and 0 = Sunday).
This function will calculate the business day in the future or past. Arguments are number of days, forward (1) or backwards(0), and a date. If no date is supplied todays date will be used:
// returned $date Y/m/d
function work_days_from_date($days, $forward, $date=NULL)
{
if(!$date)
{
$date = date('Y-m-d'); // if no date given, use todays date
}
while ($days != 0)
{
$forward == 1 ? $day = strtotime($date.' +1 day') : $day = strtotime($date.' -1 day');
$date = date('Y-m-d',$day);
if( date('N', strtotime($date)) <= 5) // if it's a weekday
{
$days--;
}
}
return $date;
}
What you need to do is:
Convert the provided date into a timestamp.
Use this along with the or w or N formatters for PHP's date command to tell you what day of the week it is.
If it isn't a "business day", you can then increment the timestamp by a day (86400 seconds) and check again until you hit a business day.
N.B.: For this is really work, you'd also need to exclude any bank or public holidays, etc.
I stumbled apon this thread when I was working on a Danish website where I needed to code a "Next day delivery" PHP script.
Here is what I came up with (This will display the name of the next working day in Danish, and the next working + 1 if current time is more than a given limit)
$day["Mon"] = "Mandag";
$day["Tue"] = "Tirsdag";
$day["Wed"] = "Onsdag";
$day["Thu"] = "Torsdag";
$day["Fri"] = "Fredag";
$day["Sat"] = "Lørdag";
$day["Sun"] = "Søndag";
date_default_timezone_set('Europe/Copenhagen');
$date = date('l');
$checkTime = '1400';
$date2 = date(strtotime($date.' +1 Weekday'));
if( date( 'Hi' ) >= $checkTime) {
$date2 = date(strtotime($date.' +2 Weekday'));
}
if (date('l') == 'Saturday'){
$date2 = date(strtotime($date.' +2 Weekday'));
}
if (date('l') == 'Sunday') {
$date2 = date(strtotime($date.' +2 Weekday'));
}
echo '<p>Næste levering: <span>'.$day[date("D", $date2)].'</span></p>';
As you can see in the sample code $checkTime is where I set the time limit which determines if the next day delivery will be +1 working day or +2 working days.
'1400' = 14:00 hours
I know that the if statements can be made more compressed, but I show my code for people to easily understand the way it works.
I hope someone out there can use this little snippet.
Here is the best way to get business days (Mon-Fri) in PHP.
function days()
{
$week=array();
$weekday=["Monday","Tuesday","Wednesday","Thursday","Friday"];
foreach ($weekday as $key => $value)
{
$sort=$value." this week";
$day=date('D', strtotime($sort));
$date=date('d', strtotime($sort));
$year=date('Y-m-d', strtotime($sort));
$weeks['day']= $day;
$weeks['date']= $date;
$weeks['year']= $year;
$week[]=$weeks;
}
return $week;
}
Hope this will help you guys.
Thanks,.
See the example below:
$startDate = new DateTime( '2013-04-01' ); //intialize start date
$endDate = new DateTime( '2013-04-30' ); //initialize end date
$holiday = array('2013-04-11','2013-04-25'); //this is assumed list of holiday
$interval = new DateInterval('P1D'); // set the interval as 1 day
$daterange = new DatePeriod($startDate, $interval ,$endDate);
foreach($daterange as $date){
if($date->format("N") <6 AND !in_array($date->format("Y-m-d"),$holiday))
$result[] = $date->format("Y-m-d");
}
echo "<pre>";print_r($result);
For more info: http://goo.gl/YOsfPX
You could do something like this.
/**
* #param string $date
* #param DateTimeZone|null|null $DateTimeZone
* #return \NavigableDate\NavigableDateInterface
*/
function getNextBusinessDay(string $date, ? DateTimeZone $DateTimeZone = null):\NavigableDate\NavigableDateInterface
{
$Date = \NavigableDate\NavigableDateFacade::create($date, $DateTimeZone);
$NextDay = $Date->nextDay();
while(true)
{
$nextDayIndexInTheWeek = (int) $NextDay->format('N');
// check if the day is between Monday and Friday. In DateTime class php, Monday is 1 and Friday is 5
if ($nextDayIndexInTheWeek >= 1 && $nextDayIndexInTheWeek <= 5)
{
break;
}
$NextDay = $NextDay->nextDay();
}
return $NextDay;
}
$date = '2017-02-24';
$NextBussinessDay = getNextBusinessDay($date);
var_dump($NextBussinessDay->format('Y-m-d'));
Output:
string(10) "2017-02-27"
\NavigableDate\NavigableDateFacade::create($date, $DateTimeZone), is provided by php library available at https://packagist.org/packages/ishworkh/navigable-date. You need to first include this library in your project with composer or direct download.
I used below methods in PHP, strtotime() does not work specially in leap year February month.
public static function nextWorkingDay($date, $addDays = 1)
{
if (strlen(trim($date)) <= 10) {
$date = trim($date)." 09:00:00";
}
$date = new DateTime($date);
//Add days
$date->add(new DateInterval('P'.$addDays.'D'));
while ($date->format('N') >= 5)
{
$date->add(new DateInterval('P1D'));
}
return $date->format('Y-m-d H:i:s');
}
This solution for 5 working days (you can change if you required for 6 or 4 days working). if you want to exclude more days like holidays then just check another condition in while loop.
//
while ($date->format('N') >= 5 && !in_array($date->format('Y-m-d'), self::holidayArray()))

Categories