Get Next and Previous Day with PHP

get next and previous day with PHP

date('Y-m-d', strtotime('+1 day', strtotime($date)))

Should read

date('Y-m-d', strtotime(' +1 day'))

Update to answer question asked in comment about continuously changing the date.

<?php
$date = isset($_GET['date']) ? $_GET['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'));
?>

<a href="?date=<?=$prev_date;?>">Previous</a>
<a href="?date=<?=$next_date;?>">Next</a>

This will increase and decrease the date by one from the date you are on at the time.

how to get previous day in PHP

Use this-

date('Y/m/d',strtotime("-1 days"));

Or Use DateTime class like this-

$date = new DateTime();
echo $date->modify("-1 days")->format('Y-m-d');

php get the upcoming date from month of day

You need to do the following:

If today is <= from the day you want, just add (day - today) days,
else, add one month to the current day and subtract (today - day).

<?php

$day = 19;

echo nextOccurrence($day);

function nextOccurrence($day) {
$currentDay = date('d');
if ($day > $currentDay) {
// Get the timestamp of $day in this month
$date = strtotime('+' . ($day - $currentDay) . ' days');
} else {
// Get the timestamp of the current day in next month, and subtract the days difference
$date = strtotime('+1 month -' . ($currentDay - $day) . ' days');
}
return date('Y-m-d', $date);
}

?>

php date function, for previous/next day

Look into DateTime relative formats:

<?php
$date = new DateTime('2006-12-12');
$date->modify('tomorrow');
echo $date->format('M m, l');

$date->modify('yesterday');
echo $date->format('M m, l');
?>

How to find the last day of the month from date?

t returns the number of days in the month of a given date (see the docs for date):

$a_date = "2009-11-23";
echo date("Y-m-t", strtotime($a_date));

How to display next and previous month via PHP

From my understanding, you need to display some links to the working days of the current month and a link to the previous and next months.

The below code requires PHP5.4 or higher.

The following function will return links to working days in a month:


const DATE_FOMAT = 'Y-m-d';
const LINK_SEPARATOR = " | ";

// get the reference date from the Query string so that we can print the calendar for any month
$refDateStr = $_GET['dt'] ?? date(DATE_FOMAT); // this requires PHP 7.1 or above

// this function will return the links to all working days
// in the given month (based on the $refDateStr)
function getDateLinks($refDateStr) {
try {

// first we check if the given date is valid or not
$refDateDt = date_create_from_format(DATE_FOMAT, $refDateStr);

if (!$refDateDt || $refDateDt->format(DATE_FOMAT) !== $refDateStr) {
// date is not valid. We cannot proceed with this date
throw new Exception('Invalid date supplied. Dates should be in ' . DATE_FOMAT .'.');
}

// now lets find first and last days of the given month
// for convenience, the start date is set to the last date of past month
$startDate = new DateTime($refDateDt->modify('first day of this month')->format(DATE_FOMAT));
$startDate->sub(new DateInterval("P1D"));

$endDate = new DateTime($refDateDt->modify('last day of this month')->format(DATE_FOMAT));

$dateLink = null;
// lets create the date links
while ($startDate < $endDate) {
$startDate->add(new DateInterval("P1D"));
// exclude weekends... :-)
if (in_array($startDate->format('D'), ['Sat', 'Sun']))
continue;
$dateLink .= getDateLinksFormated($startDate);
}
return rtrim($dateLink, LINK_SEPARATOR);
} catch (Exception $exception) {
echo $exception->getMessage();
}
}

Let's decide how to show the date links

// This function create the date links
function getDateLinksFormated(DateTime $dt):string
{
return sprintf(
"<a href='?%s'>%s</a>%s",
$dt->format(DATE_FOMAT),
$dt->format('d'),
LINK_SEPARATOR
);
}

Now it's time to show your calendar!!!

// Link to the past month
echo "<a href='?dt=" . date(DATE_FOMAT, strtotime('-1 month', strtotime($refDateStr))) . "'><< PREV MONTH </a>";

// Link to the next month
echo getDateLinks($refDateStr);

// Link to the next month
echo "<a href='?dt=" . date(DATE_FOMAT, strtotime('+1 month', strtotime($refDateStr))) . "'> NEXT MONTH >></a>";

Read more about PHP DateTime Class here

How to get Previous date in PHP?

Test the below code:

<?php
$chkoutdate = '2016-06-23';
$PreviousDate = date('Y-m-d', strtotime($chkoutdate.' - 1 day'));
echo $PreviousDate;
?>

Output:
2016-06-22

how can I get last week' date range in php?

You can use strtotime()

$previous_week = strtotime("-1 week +1 day");

$start_week = strtotime("last sunday midnight",$previous_week);
$end_week = strtotime("next saturday",$start_week);

$start_week = date("Y-m-d",$start_week);
$end_week = date("Y-m-d",$end_week);

echo $start_week.' '.$end_week ;

UPDATE

Changed the code to handle sunday. If the current day is sunday then - 1 week will be previous sunday and again getting previous sunday for that will go the one week back.

$previous_week = strtotime("-1 week +1 day");

In addition if we need to find the current week and next week date
range we can do as

Current week -

$d = strtotime("today");
$start_week = strtotime("last sunday midnight",$d);
$end_week = strtotime("next saturday",$d);
$start = date("Y-m-d",$start_week);
$end = date("Y-m-d",$end_week);

Next Week -

$d = strtotime("+1 week -1 day");
$start_week = strtotime("last sunday midnight",$d);
$end_week = strtotime("next saturday",$d);
$start = date("Y-m-d",$start_week);
$end = date("Y-m-d",$end_week);

Get the last day of the last month and the first day of next month in PHP

Use below code:

<?php

$month_end = new DateTime("last day of last month");
$month_ini = new DateTime("first day of next month");

echo $month_end->format('Y-m-d'); // will print, Last day of last month
echo $month_ini->format('Y-m-d'); // will print First day of next month

?>

With Datetime object with user defined date (Custom date)

# with datetime object
$d1 = new DateTime('2019-12-01');
$d1 -> modify('last day of last month');
echo $d1 -> format('d.m.Y'), "\n";

$d2 = new DateTime('2019-12-01');
$d2 -> modify('first day of next month');
echo $d2 -> format('d.m.Y'), "\n";

Output:
30.11.2019
01.01.2020


Related Topics



Leave a reply



Submit