GoFuckYourself.com - Adult Webmaster Forum

GoFuckYourself.com - Adult Webmaster Forum (https://gfy.com/index.php)
-   Fucking Around & Business Discussion (https://gfy.com/forumdisplay.php?f=26)
-   -   Tech Date() countdown until last Sunday of the month? (https://gfy.com/showthread.php?t=1355599)

Publisher Bucks 06-16-2022 08:21 PM

Date() countdown until last Sunday of the month?
 
I have the following code:

Quote:

$dt_end = new DateTime('June 26, 2022');
$remain = $dt_end->diff(new DateTime());
echo $remain->d . ' days and ' . $remain->h . ' hours';
How would I go about changing it so that each month, it will display the days until the last Sunday of the month without having to put the date in the code?

I've searched a few different places and can't find anything specific to what I need :(

Hopefully that makes sense?

TIA for any help you can offer.

baodb 06-16-2022 08:28 PM

Did you try 'last Sunday of the month'? PHP surprisingly can parse a lot of these things and do the heavy lifting for you

sarettah 06-16-2022 08:33 PM

Google is your friend.

https://rosettacode.org/wiki/Find_th...each_month#PHP

Like baodb said, you can use the string "last sunday of month year" in the date function to get the date of the last sunday.

date('Y-m-d', strtotime('last sunday of ' . $month . ' ' . $year)

Here is an example they have looping through and getting the last sunday of every month.

// Created with PHP 7.0

function printLastSundayOfAllMonth($year)
{
$months = array(
'January', 'February', 'March', 'April', 'June', 'July',
'August', 'September', 'October', 'November', 'December');

foreach ($months as $month) {
echo $month . ': ' . date('Y-m-d', strtotime('last sunday of ' . $month . ' ' . $year)) . "\n";
}
}

printLastSundayOfAllMonth($argv[1]);

.

Publisher Bucks 06-16-2022 08:59 PM

I swear I searched for this in Google and Sitepoint and nothing came up :/

Thank you both.

fuzebox 06-16-2022 09:38 PM

Really never came across strtotime??


All times are GMT -7. The time now is 08:53 PM.

Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2025, vBulletin Solutions, Inc.
©2000-, AI Media Network Inc123