Display date that changes every 15th day of the month - php

I'm relatively new to php and is currently looking for a way to display a date that adds 1 month on current date and adds 2 months every 15th day of every month.
For example:
Current Date: January 13, 2022
Display Date: February 2022
on January 15, 2022 the date to be displayed is March 2022 (which will be the displayed until February 14, 2022)
on February 15, 2022 the date to be displayed is April 2022
<?php
$today = date("D");
$date = date("F Y", strtotime(" +1 months"));
if ($today >= "15") {
$d=strtotime("+2 Months");
echo date("F Y", $d);
} else {
echo $date;
}
?>
Thanks in advance for your help.

Your code is almost fine.
data("D") instead of returning 15 will return the day string of the week (mon, wed, etc) so when you compare date("D") with 15 you are comparing "mon" with 15.. (not right). You have to use "d" instead (lowercase d)
This is my version of your same code
if (date("d") >= 15) {
$show_date = date("F Y", strtotime("+2 Month"));
} else {
$show_date = date("F Y", strtotime("+1 Month"));
}
echo $show_date;
Or if you prefer 1 line solution:
echo date("F Y", strtotime("+" . (date("d") >= 15 ? 2 : 1) . " Month"));

Related

Only future dates with PHP strtotime()

PHP's strtotime() uses the current year by default. How to get only future dates?
echo date('l d. M Y', strtotime('first sunday of april')); // Sunday 03. Apr 2016
I can't manage to get the next first Sunday of April. The date must not be in the past and it must be always relative (no 2017 or 2018 hardcoded).
echo date('l d. M Y', strtotime('first sunday of next april')); // fails
echo date('l d. M Y', strtotime('first sunday of april next year')); // wrong from January until that Sunday in April
I think I could do it in multiple steps or create a function to check, if current time is before/after the first Sunday and insert a 'next year' at the end.
But I was wondering if there is a simple solution with strtotime()
I dont think this is particularly elegant, but it works, and I hope it is what you were looking for?
echo date('l d. M Y', strtotime('first sunday of april', strtotime('first day of next year')));
However this seems like a much better, maintainable, readable solution
$d = new DateTime();
$d->modify( 'first day of next year');
echo $d->format('l d. M Y') . PHP_EOL;
$d->modify( 'first sunday of april');
echo $d->format('l d. M Y') . PHP_EOL;
Which gives
Tuesday 01. Aug 2017
Sunday 02. Apr 2017
The echo of the year change date, you would not need to do, its there just to prove that the year changed
I came here looking for a solution to the title of this post. I wanted to get the future date for a strtotime result every time.
date("Y-m-d", strtotime("Jan 2"));
If today's date is Jan 1, 2018, it will return the future date of 2018-01-02, but if today's date is Jan 3, 2018, it will return the same date (which is now in the past). In that case, I would prefer to get back 2019-01-02.
I know the OP said they didn't want a function, but that seemed like the simplest solution. So, here's the quick function I made to get the next future strtotime that matches.
function future_strtotime($d){
return (strtotime($d)>time())?strtotime($d):strtotime("$d +1 year");
}
Get that good date using...
date("Y-m-d", future_strtotime("Jan 2"));
Here's a more robust solution. It replaces strtotime but requires a second parameter -- a string of either past or future and offsets according.
<?php
function strtotimeForce($string, $direction) {
$periods = array("day", "week", "month", "year");
if($direction != "past" && $direction != "future") return strtotime($string);
else if($direction == "past" && strtotime($string) <= strtotime("now")) return strtotime($string);
else if($direction == "future" && strtotime($string) >= strtotime("now")) return strtotime($string);
else if($direction == "past" && strtotime($string) > strtotime("now")) {
foreach($periods as $period) {
if(strtotime($string) < strtotime("+1 $period") && strtotime($string, strtotime("-1 $period"))) {
return strtotime($string, strtotime("-1 $period"));
}
}
return strtotime($string);
}
else if($direction == "future" && strtotime($string) < strtotime("now")) {
foreach($periods as $period) {
if(strtotime($string) > strtotime("-1 $period") && strtotime($string, strtotime("+1 $period")) > strtotime("now")) {
return strtotime($string, strtotime("+1 $period"));
}
}
return strtotime($string);
}
else return strtotime($string);
}

Echo every third Thursday starting 3 March 2016

Im developing a website for a company using wordpress
They have a board meeting every 3rd Thursday starting 3 March 2016
Im trying to create a shortcode
I want to echo when the next meeting will occur.
This is what ive got so far, but that just isnt correct.
// ECHO next board_meeting - Use [next_board_meeting]
add_shortcode( 'next_board_meeting', 'next_board_meeting_shortcode' );
function next_board_meeting_shortcode( $str ) {
echo date_i18n('l j F', strtotime('next thursday +2 week'));
}
I've been searching the web for something like this , but here I am, asking for your help.
Is there a simple way to do this , or do I need to create a complex php script ?
Bear in mind, I just learned how to do simple tasks in php.
This code will get you third Thursday of current month ..
If the current date has passed the 3rd Thurs then It will show 3rd Thurs of next month.. If current Month is December and has passed 3rd Thurs then it will show 3rd Thurs in next year in Jan
<?php
$current_date = strtotime(date("d.m.Y"));
//$final = date("Y-m-d", strtotime("+1 month", $time));
$FullMonth = date('F',$current_date);
$FullYear = date('Y',$current_date);
$Third_Thus = date('d F Y', strtotime('third thursday of '.$FullMonth.' '.$FullYear));
$ThirdThus_tmstamp = strtotime($Third_Thus);
if($current_date <= $ThirdThus_tmstamp){
echo date('d F Y', strtotime('third thursday of '.$FullMonth.' '.$FullYear));
}
else{
if($FullMonth != 'December'){
$FullMonth = date('F',strtotime("+1 month", $current_date));
$FullYear = date('Y',$current_date);
} else{
$Next_Year = strtotime("+1 year", $current_date);
$FullYear = date('Y',$Next_Year);
$FullMonth = date('F',strtotime("+1 month", $Next_Year));
}
echo date('d F Y', strtotime('third thursday of '.$FullMonth.' '.$FullYear));
}?>
This will show only one 3rd Thurs of either Current Month or Next
Month depending on the current date.. If you want the 3rd Thurs of
next 2-3 month then you can make some changes I guess now !!!
Should work with this simple shortcode:
add_shortcode( 'next_board_meeting', 'next_board_meeting_shortcode' );
function next_board_meeting_shortcode( $str ) {
if(strtotime("3 thursday", strtotime(date('Y-m-01 00:00:00'))) > time())
echo date('Y-m-d', strtotime("3 thursday", strtotime(date('Y-m-01 00:00:00'))));
else
echo date('Y-m-d', strtotime("3 thursday", strtotime("next month", strtotime(date('Y-m-01 00:00:00')))));
}
Demo: https://eval.in/530446

What happening to php date function [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP date() and strtotime() return wrong months on 31st
I have this code and it outputs something strange i think. So, what i am doing wrong here.
<?php
$sP1 = date('m Y');
$sP2 = date('m Y', strtotime('+01 month'));
$sP3 = date('m Y', strtotime('+02 month'));
$sP4 = date('m Y', strtotime('+03 month'));
echo $sP1.'<br>';
echo $sP2.'<br>';
echo $sP3.'<br>';
echo $sP4.'<br>';
?>
and this outputs
05 2012
07 2012
07 2012
08 2012
i think the second one should be
06 2012
Anybody know any solution?
Today is the 31st next month only has 30 days so it would be 7/12 in 1 month from today
assuming that today is May 31 2012
date('m Y') == 05 2012
date('m Y', strtotime('+1 month')) == 07 2012 because june has 30 days
date('m Y', strtotime('+2 month')) == 07 2012
date('m Y', strtotime('+3 month')) == 08 2012
date('m Y', strtotime('+4 month')) == 10 2012
I would take today's date and find the first day of the month then add a month to that if you are doing something that needs to get each month
As others have said, it is because today is the 31st and +1 month equals June-31 which changes to Jul-1. If you include the day in the date string, you can see exactly this.
<?php
$sP1 = date('m-d-Y');
$sP2 = date('m-d-Y', strtotime('+01 month'));
$sP3 = date('m-d-Y', strtotime('+02 month'));
$sP4 = date('m-d-Y', strtotime('+03 month'));
echo $sP1."\n";
echo $sP2."\n";
echo $sP3."\n";
echo $sP4."\n";
/* Outputs:
05-31-2012
07-01-2012
07-31-2012
08-31-2012
*/
?>
strtotime though can take the start date as part of the string so as King suggested, calculate the +N months from the first. So a string like May-1-2012 +01 month such as:
<?php
$sP1 = date('m Y');
$sP2 = date('m Y', strtotime(date('M-1-Y').' +01 month'));
$sP3 = date('m Y', strtotime(date('M-1-Y').' +02 month'));
$sP4 = date('m Y', strtotime(date('M-1-Y').' +03 month'));
echo $sP1."\n";
echo $sP2."\n";
echo $sP3."\n";
echo $sP4."\n";
/* Outputs:
05 2012
06 2012
07 2012
08 2012
*/
?>
http://codepad.org/auYLHvDI
It is working as intended. In a nutshell, it is because what is "one month" from May 31? June 30? August 1?
My suggestion is that if you need sequential months, calculate the offset from the start of the current month, not the current day. Or compose the date that you're looking for manually using the month, day, and year parts broken up.

Finding date range for current week, month and year

Suppose I have a date available with me:
2011-04-05 (i.e., 5th April, 2011)
I want to find date range for Current Week, Month and Year
Current Week: 3rd April to 9th April
Current Month: 1st April to 30 April
Current Year: 1st Jan to 31 Dec
I do understand that current year would be always 1st Jan to 31Dec, but what about current month and week, How can I find it?
Edit:
How can I find date, which is 10 days earlier or later from a given date.
Example:
Suppose today's date is 6th April, 2011
10 day's earlier: 28 March, 2011
10 day's later: 15 April, 2011
Any thoughts on this, guys?
function rangeMonth ($datestr) {
date_default_timezone_set (date_default_timezone_get());
$dt = strtotime ($datestr);
return array (
"start" => date ('Y-m-d', strtotime ('first day of this month', $dt)),
"end" => date ('Y-m-d', strtotime ('last day of this month', $dt))
);
}
function rangeWeek ($datestr) {
date_default_timezone_set (date_default_timezone_get());
$dt = strtotime ($datestr);
return array (
"start" => date ('N', $dt) == 1 ? date ('Y-m-d', $dt) : date ('Y-m-d', strtotime ('last monday', $dt)),
"end" => date('N', $dt) == 7 ? date ('Y-m-d', $dt) : date ('Y-m-d', strtotime ('next sunday', $dt))
);
}
print_r (rangeMonth('2011-4-5')); // format: YYYY-M-D
print_r (rangeWeek('2011-4-5'));
output for rangeMonth()
Array
(
[start] => 2011-04-01
[end] => 2011-04-30
)
output for rangeWeek()
Array
(
[start] => 2011-04-04
[end] => 2011-04-08
)
Notice: functions like getdate(), date(), etc. throw Warning if default time zone is not set in php.ini.
you can use strtotime
example :
date('d.m.Y',strtotime('last day of this month'))
date('d.m.Y',strtotime('last monday')) // for first day of this week
This solution is simple, and it takes in consideration when the current day is Monday or Sunday.
NOTE: Using strtotime('last monday') may work if the day is other than Monday, otherwise it will return the previous Monday. Because of that we should use strtotime('last monday', strtotime('tomorrow')) and it will work for any day of the current week =)
Solution:
$monday = strtotime('last monday', strtotime('tomorrow'));
$sunday = strtotime('+6 days', $monday);
echo "<P>". date('d-M-Y', $monday) . " to " . date('d-M-Y', $sunday) . "</P>";
The following code will give you the start and last date of a week:
$today = getdate();
print_r($today);
echo "<br/>";
$weekStartDate = $today['mday'] - $today['wday'];
$weekEndDate = $today['mday'] - $today['wday']+6;
echo "<br/>";
echo "<br/>";
echo "week start date:".$weekStartDate;
echo "<br/>";
echo "week end date:".$weekEndDate;
Hope it helps...
Check out the getdate function, there are a few examples of how to use it on the manual page I linked. I think it will return everything you're looking for,
Working example it properly handles the Monday issue
<?php
$monday = strtotime("last monday");
$monday = date('w', $monday)==date('w') ? $monday+7*86400 : $monday;
$sunday = strtotime(date("Y-m-d",$monday)." +6 days");
$this_week_sd = date("Y-m-d",$monday);
$this_week_ed = date("Y-m-d",$sunday);
echo "Current week range from $this_week_sd to $this_week_ed ";
?>
Today is Tuesday, August 27th 2019
This week is from Monday, August 26th 2019 to Sunday, September 1st 2019
php > $date = new DateTime('Sunday');
php > echo $date->format('Y-m-d H:i:s');
2019-09-01 00:00:00
php > $date = new DateTime('Tuesday');
php > echo $date->format('Y-m-d H:i:s');
2019-08-27 00:00:00
php > $date = new DateTime('Monday');
php > echo $date->format('Y-m-d H:i:s');
2019-09-02 00:00:00
php > $date = new DateTime('Monday this week');
php > echo $date->format('Y-m-d H:i:s');
2019-08-26 00:00:00

PHP - Date function - finding the previous week

In my application a week is defined from Monday 12:00:00 AM to Sunday 11:59:59 PM
Whenever a user visits my site - I need to find the previous weeks date range and show him results based on that. It sounds simple but I'm lost.
To give you scenarios -
- March 1st Monday 12:00:00 AM to March 7th Sunday 12:59:59 PM is the week.
Now when a user visits the website on 8th March or 10th March or 12th March - based on the current date I should be able to get the previous week date range ie start date March 1st and end date March 7th.
But if the user visits the site say on 16th March - the date range I would need is March 8th to March 15th.
How can I do this in PHP.
Thanks
You could try doing it with timestamps, but that gets messy with timezone changes (for example, CET -> CEST). I'd use the DateTime class:
$d = new DateTime();
$weekday = $d->format('w');
$diff = 7 + ($weekday == 0 ? 6 : $weekday - 1); // Monday=0, Sunday=6
$d->modify("-$diff day");
echo $d->format('Y-m-d') . ' - ';
$d->modify('+6 day');
echo $d->format('Y-m-d');
The strtotime function is very handy here:
$mondayStr = "last monday";
if (date('N') !== '1') { // it's not Monday today
$mondayStr .= " last week";
}
$monday = strtotime($mondayStr);
echo date('r', $monday); // Mon, 22 Feb 2010 00:00:00 +1000
$sunday = strtotime('next monday', $monday) - 1;
echo date('r', $sunday); // Sun, 28 Feb 2010 23:59:59 +1000
function get_week_start($year, $month, $day)
{
$timestamp = mktime(0, 0, 0, $month, $day, $year);
return date('F j Y', $timestamp = mktime(0, 0, 0, $month, date('d', $timestamp)-date('w', $timestamp), $year));
}
You could perhaps add the next 6 days and you have it.
There is a user function for this in the PHP Documentation.
GMT version
$prev_monday_t = time() - (gmdate('N') + 6) * 86400;
$prev_sunday_t = time() - gmdate('N') * 86400;
echo gmdate('Y-m-d H:i:s', $prev_monday_t ).' '.gmdate('Y-m-d H:i:s', $prev_sunday_t );
Local version
$prev_monday_t = time() - (date('N') + 6) * 86400;
$prev_sunday_t = time() - date('N') * 86400;
echo date('Y-m-d H:i:s', $prev_monday_t ).' '.date('Y-m-d H:i:s', $prev_sunday_t );

Categories