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);
}
Related
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
I want to display something every second week for 5 days. So this week it's Feb 8 to Feb 12, and I want it to show again Feb 22 to Feb 26 and continue on indefinately, will this code work? thanks.
$StartDate = strtotime('2016-01-25');
$CurDate = date("Y-m-d");
$NextDate = date("Y-m-d", strtotime("+2 week", $StartDate));
$EndDate = date("Y-m-d", strtotime("+18 days", $StartDate));
while ($CurDate > $NextDate && $CurDate > $EndDate ) {
$NextDate = date("F j", strtotime("+2 week", strtotime($NextDate)));
$EndDate = date("F j", strtotime("+18 days", strtotime($EndDate)));
}
<?php if ( $CurDate >= $NextDate && $NextDate <= $EndDate ) { echo "..."; } ?>
I think the code is right but I'm not sure if it will loop.
Thanks.
It is best to run your code and attempt to debug it yourself before asking for help, you never know what you'll find on your own!
As for your code, I don't see any immediate issues, however I'm not the PHP interpreter!
It might, but you might be better off using the date() function to get the current week of year:
if (((int) date('W')) % 2 === 0)
{
// display message
}
date('W') will return the current week number (out of 52 weeks per year). The above code would display the message on even weeks and hide it on odd weeks. You could check for === 1 to display the message on odd weeks instead.
Here's a summary of the issue: On Sundays, strtotime('this week') returns the start of next week.
In PHP, the week seems to start on Monday. But, on any day except Sunday, this code
echo date('Y-m-d', strtotime('monday this week', strtotime('last sunday')));
Outputs the date of this week's Monday, when it seems like it should be outputting last weeks Monday. It seems like, in this case, PHP is treating both Sunday and Monday as the the start of the week. It's now Monday, Dec 10, 2012, or 2012-12-10. date('Y-m-d', strtotime('sunday last week')) returns 2012-12-09 - yesterday.
Is this a bug, or am I missing something? It seems like a bug this obvious should be fairly well known, but I can't find anything about it. Is the only way to get the start of the week to use some special handling for Sundays?
$week_offset = (int) 'sunday' == date('l');
$week_start = strtotime("-$week_offset monday"); // 1 or 0 Mondays ago
As far as I can tell, this is a bug. I see no logical reason why strtotime('this week'); should return a future date. This is a pretty major bug. In my particular case, I had a leaderboard that showed the users with the most points since the beginning of the week. But on Sundays, it was empty because strtotime returned a timestamp for a future date. I was doubtful, because just I don't know how this could have gone unnoticed, but I couldn't find any other reports of this bug.
Thanks for all your time and help, folks.
This answer is late, but it's something that I've been struggling with. Every solution I've tried so far has malfunctioned for one reason or another. This is what I ended up with that worked for me. (though it may be look pretty, it at least works).
$thisMonday = strtotime('next Monday -1 week', strtotime('this sunday'));
Here is how you can get Monday of current week:
echo date("Y-m-d", strtotime(date('o-\\WW')));
It's not ideal but this is what I resorted to using:
if(date('N') == 7) {
$date = date('Y-m-d',strtotime('monday last week'));
} else {
$date = date('Y-m-d',strtotime('monday this week'));
}
I think the only problem with your coding is TimeZone.
Solution:
Set your own time Zone. Here is the example of my own time zone:
Example
date_default_timezone_set('Asia/Kolkata');
Set the above line before calling any time function.
Have a nice day.
I think instead of trying
echo date('Y-m-d', strtotime('monday this week', strtotime('last sunday')));
you should try
echo date('Y-m-d', strtotime('monday last week'));
Try this code
// set current date
$date = date("m/d/Y");
$ts = strtotime($date); // also $ts = time();
// find the year and the current week
$year = date('o', $ts);
$week = date('W', $ts);
// print week for the current date
$i = 1; // 1 denotes the first day of week
$ts = strtotime($year.'W'.$week.$i);
echo $day = date("l", $ts); // generate the name of day
echo "<br>";
echo $day = date("Y-m-d", $ts); // generate the date
You will get the the date of current week, whether you are on monday you will get the date of that monday.
If you want the most recent monday:
function mostRecentMonday(){
if(date("w") == 1){
return strtotime("midnight today");
} else {
return strtotime("last monday");
}
}
Easy to modify to use DateTime, or, to even specify a different date to use as the base.
Based on Bryant answer :
$first_week_date = date('d F Y', strtotime('next Monday -1 week', strtotime('this sunday')));
$last_week_date = date('d F Y', strtotime('next Monday -1 week + 6 days', strtotime('this sunday')));
This is for thos looking for a friendly solution that works with any day.
function getWeekStart($week_start_day = "Monday") {
$week_days = array("Sunday"=>0,"Monday"=>1,"Tuesday"=>2,"Wednesday"=>3,"Thursday"=>4,"Friday"=>5,"Saturday"=>6,);
if(!isset($week_days[$week_start_day])) {
return false;
} else {
$start_day = $week_days[$week_start_day];
$today = date("w");
$one_day = (60 * 60 * 24);
if($today < $start_day) {
$days_difference = 7 - ($start_day - $today);
} else {
$days_difference = ($today - $start_day);
}
$week_starts = strtotime(date("Y-m-d 00:00:00")) - ($one_day * $days_difference);
return $week_starts;
}
}
//Test: If today is Monday, it will return today's date
echo date("Y-m-d H:i:s", getWeekStart("Monday"));
I've looked at at least 2 dozen topics about this and haven't really found a good answer yet, so I come to you to ask once again for answers regarding the dreaded topic of Repeating Events.
I've got Daily, Weekly, Monthly, and Yearly repeats working out fine for now (I still need to revamp the system with Exception events and whatnot, but it works for the time being). But, we want to be able to add the ability to repeat events on the (1st,2nd,3rd,4th,5th) [Sun|Mon|Tue|Wed|Thu|Fri|Sat] of every month, every other month, and every three months.
Now, if I can just understand the logic for the every month, I can figure out the every other month and the every three months.
Here's a bit of what I have so far (note: I'm not saying I have the best way of doing it or anything, but the system is one we update very slowly over time when we aren't busy with other projects, so I make the code more efficient as I have the time).
First I get the starting and ending dates formatted for date calculations:
$ending = $_POST['end_month'] . "/" . $_POST['end_day'] . "/" . substr($_POST['end_year'], 2, 2);
$starting = $_POST['month'] . "/" . $_POST['day'] . "/" . substr($_POST['year'], 2, 2);
Then I get the difference between those two to know how many times to repeat using a function I'm fairly certain I found on here some time ago and dividing that amount by 28 days to get just about how many TIMES it needs to repeat so that there is one a month:
$repeat_number = date_diff($starting, $ending) / 28;
//find the difference in DAYS between the two dates
function date_diff($old_date, $new_date) {
$offset = strtotime($new_date) - strtotime($old_date);
return $offset/60/60/24;
}
Then I add the (1st,2nd,etc...) part to the [Sun|Mon|etc...] part to figure out what they are looking for giving me somehthing like 'first Sunday' :
$find = $_POST['custom_number']. ' ' . $_POST['custom_day'];
Then I use a loop that runs the number of times this needs to repeat (the $repeat_number from above):
for($m = 0; $m <= $repeat_number; $m++) {
if($m == 0) {
$month = date('F', substr($starting,0,2));
} else {
$month = date('F', strtotime($month . ' + ' . $m . ' months'));
}
$repeat_date = strtotime($find . ' in ' . $month);
}
Now, I'm aware this code doesn't work, I at one time did have code that turned up the correct month and year for the repeat, but wouldn't necessarily find the first tuesday or whatever it was that was being looked for.
If anyone could point me back in the right direction, it would be most appreciated. I've been lurking in the community for some time now, just started trying to actively participate recently.
Thanks in advance for any input or advice you can provide.
Here's a possible alternative for you. strtotime is powerful, and the syntax includes really useful bits like comprehensive relative time and date wording.
You can use it to generate the first through Nth specific weekdays of a specific month by using the format " of ". Here's an example using date_create to invoke a DateTime object, but regular old strtotime works the same way:
php > $d = date_create('Last Friday of March 2011'); if($d instanceof DateTime) echo $d->format('l F d Y H:i:s');
Friday March 25 2011 00:00:00
php > $d = date_create('First Friday of March 2011'); if($d instanceof DateTime) echo $d->format('l F d Y H:i:s');
Friday March 04 2011 00:00:00
php > $d = date_create('First Sunday of March 2011'); if($d instanceof DateTime) echo $d->format('l F d Y H:i:s');
Sunday March 06 2011 00:00:00
php > $d = date_create('Fourth Sunday of March 2011'); if($d instanceof DateTime) echo $d->format('l F d Y H:i:s');
Sunday March 27 2011 00:00:00
php > $d = date_create('Last Sunday of March 2011'); if($d instanceof DateTime) echo $d->format('l F d Y H:i:s');
Sunday March 27 2011 00:00:00
It will also overflow the month, if you ask for an invalid one:
php > $d = date_create('Ninth Sunday of March 2011'); if($d instanceof DateTime) echo $d->format('l F d Y H:i:s');
Sunday May 01 2011 00:00:00
Note that it only works with the ordinal number wording. You can't pass "1st" or "3rd", unfortunately.
Once you use this to grab the proper Nth weekday, you can then simply add the number of needed weekdays to skip (7 for one week, 14 for two, 21 for three, etc) as required until the designated end date or designated number of weeks has passed. Once again, strtotime to the rescue, using the second argument to chain together relativeness:
php > echo date('l F d Y H:i:s', strtotime('+14 days', strtotime('First Thursday of March 2011')));
Thursday March 17 2011 00:00:00
(My local copy also accepted '+14 days First Thursday of March 2011', but that felt kind of weird.)
I have a more simple method, although it may seem hack-ish.
What we can do is take the start date, and infer it's "n" value from it by taking the ceiling of its day divided by 7.
For example, if your start date is the 18th, we divide by 7 and take the ceiling to infer that it is the 3rd "whatever-day" of the month.
From there we can simply repeatedly add 7 days to this to get a new date, and whenever the new date's ceilinged 7-quotient is 3, we've hit the next "nth whatever-day" of the month.
Some code can be seen below:
$day_nth = ceil(date("j", strtotime($date))/7);
for ($i = 0; $i < $number_of_repeats; $i++) {
while (ceil(date("j", strtotime($date))/7) != $day_nth)
$date = date("Y-m-d", strtotime("+7 days", strtotime($date)));
/* Add your event, or whatever */
/* Now we increment date by 7 days before the loop starts again,
** otherwise the while loop would never execute and we'd just be
** adding to the same date repeatedly. */
$date = date("Y-m-d", strtotime("+7 days", strtotime($date)));
}
What's nice about this method is that we don't even have to concern ourselves with which day of the week we're dealing with. We just infer everything from the start date and away we go.
I make it like that:
//$typeOfDate = 0;
$typeOfDate = 1;
$day = strtotime("2013-02-01");
$to = strtotime(date("Y-m-d", $day) . " +6 month");
if ($typeOfDate == 0) { //use the same day (number) of the month
$dateArray[] = date("Y-m-d", $day);
$event_day_number = date("d", $day);
while ($day <= $to) {
$nextMonth = date("m", get_x_months_to_the_future($day));
$day = strtotime(date("Y-" . $nextMonth . "-d", $day));
$dateArray[] = date("Y-m-d", $day);
}
}
if ($typeOfDate == 0) { //use the same day (like friday) of the month
$dateArray[] = date("Y-m-d", $day);
$monthEvent = date("m", $day);
$day = strtotime(date("Y-m-d", $day) . " +4 weeks");
$newMonthEvent = date("m", $day);
if ($monthEvent == $newMonthEvent) {
$day = strtotime(date("Y-m-d", $day) . " +7 days");
}
while ($day <= $to) {
$dateArray[] = date("Y-m-d", $day);
$monthEvent = date("m", $day);
$day = strtotime(date("Y-m-d", $day) . " +4 weeks");
$newMonthEvent = date("m", $day);
if ($monthEvent == $newMonthEvent) {
$day = strtotime(date("Y-m-d", $day) . " +7 days");
}
}
}
I want to use strtotime("last Monday").
The thing is, if today IS MONDAY, what does it return?
It seems to be returning the date for the monday of last week. How can I make it return today's date in that case?
If you read the manual, there is an great example that describes exactly what you want to do http://www.php.net/manual/en/datetime.formats.relative.php
strtotime('Monday this week');
Update: There appears to be a bug introduced in newer versions of PHP where this week returns the wrong week when ran on Sundays. You can vote on the bug here: https://bugs.php.net/bug.php?id=63740
Update 2: As of May 18th 2016, this has been fixed in PHP 5.6.22, PHP 7.0.7 and PHP 7.1-dev (and hopefully remains fixed in subsequent releases) as seen here: https://bugs.php.net/bug.php?id=63740#1463570467
How can I make it return today's date in that case?
pseudocode:
if (today == monday)
return today;
else
return strtotime(...);
Btw, this trick also could work:
strtotime('last monday', strtotime('tomorrow'));
If today is Monday, strtotime("last Monday") will return a date 7 days in the past. Why don't you just check if today is Monday and if yes, return today's date and if not, return last week?
That would be a foolproof way of doing this.
if (date('N', time()) == 1) return date('Y-m-d');
else return date('Y-m-d', strtotime('last Monday'));
http://us2.php.net/manual/en/function.date.php
As it was correctly outlined in the previous answer, this trick works, but also had caveats prior to PHP 5.6.22, PHP 7.0.7 and PHP 7.1-dev:
strtotime('last monday', strtotime('tomorrow'));
// or this one, which is shorter, but was buggy:
strtotime('Monday this week');
To those, who prefer the "Jedy-way", to work with objects of the DateTime class, the solution is next:
(new \DateTime())->modify('tomorrow')->modify('previous monday')->format('Y-m-d');
or even shorter notation:
\DateTime('Monday this week')
Be carefull, because if you do the same on SQL, you don't need to have any of these tricks in mysql with addition of "tomorrow". Here's how the solution will look:
SELECT DATE_SUB(CURDATE(), INTERVAL WEEKDAY(CURDATE()) DAY) as last_monday;
Late answer, but I thought I would post up this answer (which is actually from a different but related question). It handles the scenario in the question:
function last_monday($date) {
if (!is_numeric($date))
$date = strtotime($date);
if (date('w', $date) == 1)
return $date;
else
return strtotime(
'last monday',
$date
);
}
echo date('m/d/y', last_monday('8/14/2012')); // 8/13/2012 (tuesday gives us the previous monday)
echo date('m/d/y', last_monday('8/13/2012')); // 8/13/2012 (monday throws back that day)
echo date('m/d/y', last_monday('8/12/2012')); // 8/06/2012 (sunday goes to previous week)
try it: http://codepad.org/rDAI4Scr
... or a variation that has sunday return the following day (monday) rather than the previous week, simply add a line:
elseif (date('w', $date) == 0)
return strtotime(
'next monday',
$date
);
try it: http://codepad.org/S2NhrU2Z
You can pass it a timestamp or a string, you'll get back a timestamp
Documentation
strtotime - http://php.net/manual/en/function.strtotime.php
date - http://php.net/manual/en/function.date.php
Depending on exactly what you're using it for, this may be useful. Since one second's ambiguity is OK for my requirements, I use:
date( 'Y-m-d 23:59:59', strtotime( 'last sunday' ))
to get midnight on the most recent Monday (or today if today IS Monday).
My aproach:
date_default_timezone_set('Europe/Berlin');
function givedate($weekday, $time) {
$now = time();
$last = strtotime("$weekday this week $time");
$next = strtotime("next $weekday $time");
if($now > $last) {
$date = date("d.m.Y - H:i",$next);
}
else {
$date = date("d.m.Y - H:i",$last);
}
return $date;
}
echo givedate('Wednesday', '00:52');
Or monthly
function givedate_monthly($weekday, $time) {
$now = time();
$last = strtotime("first $weekday of this month $time");
$next = strtotime("first $weekday of next month $time");
if($now > $last) {
$date = date("d.m.Y - H:i",$next);
}
else {
$date = date("d.m.Y - H:i",$last);
}
return $date;
}
echo givedate_monthly('Wednesday', '01:50');
$monday = strtotime('Monday last week');
$sunday = strtotime('+6 days', $monday);