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
Related
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);
}
I can get the Monday of this week with:
$monday = date_create()->modify('this Monday');
I would like to get with the same ease the 1st of this month. How can I achieve that?
Here is what I use.
First day of the month:
date('Y-m-01');
Last day of the month:
date('Y-m-t');
Requires PHP 5.3 to work ("first day of" is introduced in PHP 5.3). Otherwise the example above is the only way to do it:
<?php
// First day of this month
$d = new DateTime('first day of this month');
echo $d->format('jS, F Y');
// First day of a specific month
$d = new DateTime('2010-01-19');
$d->modify('first day of this month');
echo $d->format('jS, F Y');
// alternatively...
echo date_create('2010-01-19')
->modify('first day of this month')
->format('jS, F Y');
In PHP 5.4+ you can do this:
<?php
// First day of this month
echo (new DateTime('first day of this month'))->format('jS, F Y');
echo (new DateTime('2010-01-19'))
->modify('first day of this month')
->format('jS, F Y');
If you prefer a concise way to do this, and already have the year and month in numerical values, you can use date():
<?php
echo date('Y-m-01'); // first day of this month
echo "$year-$month-01"; // first day of a month chosen by you
This is everything you need:
$week_start = strtotime('last Sunday', time());
$week_end = strtotime('next Sunday', time());
$month_start = strtotime('first day of this month', time());
$month_end = strtotime('last day of this month', time());
$year_start = strtotime('first day of January', time());
$year_end = strtotime('last day of December', time());
echo date('D, M jS Y', $week_start).'<br/>';
echo date('D, M jS Y', $week_end).'<br/>';
echo date('D, M jS Y', $month_start).'<br/>';
echo date('D, M jS Y', $month_end).'<br/>';
echo date('D, M jS Y', $year_start).'<br/>';
echo date('D, M jS Y', $year_end).'<br/>';
Currently I'm using this solution:
$firstDay = new \DateTime('first day of this month');
$lastDay = new \DateTime('last day of this month');
The only issue I came upon is that strange time is being set. I needed correct range for our search interface and I ended up with this:
$firstDay = new \DateTime('first day of this month 00:00:00');
$lastDay = new \DateTime('first day of next month 00:00:00');
I use a crazy way to do this is using this command
$firstDay=date('Y-m-d',strtotime("first day of this month"));
$lastDay=date('Y-m-d',strtotime("last day of this month"));
Thats all
In php 5.2 you can use:
<? $d = date_create();
print date_create($d->format('Y-m-1'))->format('Y-m-d') ?>
Ugly, (and doesn't use your method call above) but works:
echo 'First day of the month: ' . date('m/d/y h:i a',(strtotime('this month',strtotime(date('m/01/y')))));
You can do it like this:
$firstday = date_create()->modify('first day January 2010');
using date method, we should be able to get the result.
ie; date('N/D/l', mktime(0, 0, 0, month, day, year));
For Example
echo date('N', mktime(0, 0, 0, 7, 1, 2017)); // will return 6
echo date('D', mktime(0, 0, 0, 7, 1, 2017)); // will return Sat
echo date('l', mktime(0, 0, 0, 7, 1, 2017)); // will return Saturday
I use this with a daily cron job to check if I should send an email on the first day of any given month to my affiliates. It's a few more lines than the other answers but solid as a rock.
//is this the first day of the month?
$date = date('Y-m-d');
$pieces = explode("-", $date);
$day = $pieces[2];
//if it's not the first day then stop
if($day != "01") {
echo "error - it's not the first of the month today";
exit;
}
Timestamp for start of this month and very last second of current month.
You can add 00:00:00 or just reference "today"
Alternative:
$startOfThisMonth = strtotime("first day of this month",strtotime("today"));
OR
$startOfThisMonth = strtotime("first day of this month 00:00:00");
$endOfThisMonth = strtotime("first day of next month",$startOfThisMonth)-1;
I am providing this answer as an alternative one liner if the DateTime object is not preferred
Basically, I get the current day number, reduce it by one then take that number of days from itself ("today" which automatically resets the clock to 00:00:00 too) and you get the start of the month.
$startOfMonth = strtotime("today - ".(date("j")-1)." days");
If you're using composer, you can install carbon:
composer require nesbot/carbon
This is then as simple as:
use Carbon/Carbon;
$startOfMonth = Carbon::now()->startOfMonth()->toDateTime();
I have a problem in PHP:
How can I get the next friday of a given day?
E.g.
What's the date of next friday following monday 6th April 2015?
Is there a way to pass as a parameter the wanted day to strtotime( "next friday")?
Ok, got it! Thanks to all!
The problem with my dates is that they formated like d/m/Y, and I was messing it all up.
$dt = explode("/", $_SESSION['conj']['dtEnd'][0]);
$newDate = $dt[2] ."-".$dt[1]."-".$dt[0];
$nextFriday = date ('d/m/Y', strtotime("next friday", strtotime($newDate)));
<?php
$time = strtotime('Monday, April 6, 2015');
$next = strtotime('next friday, 11:59am', $time);
echo date('l, F j', $next);
?>
I have searched and searched but can't seem to find anything that will fit what i am trying to do, and I can't seem to figure out how to make it work.
Here's the over view:
My computer club meets on the third Monday of every month, with the club board meeting one week later. On our club website they are manually changing this date every month... :(
I am trying to automate it using PHP. I have had a little luck, but for the most part it won't come out right.
Here is the code example I am using:
//Set Default Time Zone
date_default_timezone_set('America/New_York');
//Define Variables Needed
$now = date("U");
$currmonth = date("n");
$curryear = date("Y");
$monthyear = date("F Y");
$thirdmon = date('U', strtotime($monthyear.' third monday'));
$thirdtue = date('U', strtotime($monthyear.' third tuesday'));
//Check for current date before or after third monday of the current meeting month
if ($now > $thirdtue) {
$monthyear = date("F Y", strtotime('next month'));
$thirdmon = date('U', strtotime($monthyear.' third monday'));
}
echo date("l F d, Y", $thirdmon);
I added the the $thirdtue variable because checking against the $thirdmon variable was causing a problem on the actual meeting date with it jumping to the next months meeting date, so I check against the third Tues and it doesn't jump until midnight on the meeting date now.
Now the weird thing is that I was working on this on March 30, 2014 and it was showing April 21, 2014 as the next meeting date which is correct. When the time rolled to midnight, 00:00 and the date changed to March 31, 2014 the meeting date changed to May 19, 2014!?!?!
I am also having problems with a couple of other parts of the code that do some other calculations, but I figure if i can get this down then I should be able to work out the rest.
Can anyone tell me where I coming off the rails with this??
$third_monday = new DateTime('third monday of this month');
// if date has passed, get next month's third monday
if ($third_monday < new DateTime()) {
$third_monday->modify('third monday of next month');
}
echo $third_monday->format('l F d, Y');
I don't have very much experience with php but I threw this together. It's been tested, and it seems to work.
<?php
date_default_timezone_set('America/New_York');
$currmonth = date("n");
$curryear = date("Y");
$currthirdmon = date("d", strtotime("third monday", mktime(0,0,0,$currmonth,1,$curryear)));
if ($currthirdmon < date("d"))
{
$nextmeeting = date("l F d, Y", strtotime("third monday", mktime(0,0,0,$currmonth+1,1,$curryear)));
}
else
{
$nextmeeting = date("l F d, Y", strtotime("third monday", mktime(0,0,0,$currmonth,1,$curryear)));
}
print $nextmeeting;
?>
I can get the Monday of this week with:
$monday = date_create()->modify('this Monday');
I would like to get with the same ease the 1st of this month. How can I achieve that?
Here is what I use.
First day of the month:
date('Y-m-01');
Last day of the month:
date('Y-m-t');
Requires PHP 5.3 to work ("first day of" is introduced in PHP 5.3). Otherwise the example above is the only way to do it:
<?php
// First day of this month
$d = new DateTime('first day of this month');
echo $d->format('jS, F Y');
// First day of a specific month
$d = new DateTime('2010-01-19');
$d->modify('first day of this month');
echo $d->format('jS, F Y');
// alternatively...
echo date_create('2010-01-19')
->modify('first day of this month')
->format('jS, F Y');
In PHP 5.4+ you can do this:
<?php
// First day of this month
echo (new DateTime('first day of this month'))->format('jS, F Y');
echo (new DateTime('2010-01-19'))
->modify('first day of this month')
->format('jS, F Y');
If you prefer a concise way to do this, and already have the year and month in numerical values, you can use date():
<?php
echo date('Y-m-01'); // first day of this month
echo "$year-$month-01"; // first day of a month chosen by you
This is everything you need:
$week_start = strtotime('last Sunday', time());
$week_end = strtotime('next Sunday', time());
$month_start = strtotime('first day of this month', time());
$month_end = strtotime('last day of this month', time());
$year_start = strtotime('first day of January', time());
$year_end = strtotime('last day of December', time());
echo date('D, M jS Y', $week_start).'<br/>';
echo date('D, M jS Y', $week_end).'<br/>';
echo date('D, M jS Y', $month_start).'<br/>';
echo date('D, M jS Y', $month_end).'<br/>';
echo date('D, M jS Y', $year_start).'<br/>';
echo date('D, M jS Y', $year_end).'<br/>';
Currently I'm using this solution:
$firstDay = new \DateTime('first day of this month');
$lastDay = new \DateTime('last day of this month');
The only issue I came upon is that strange time is being set. I needed correct range for our search interface and I ended up with this:
$firstDay = new \DateTime('first day of this month 00:00:00');
$lastDay = new \DateTime('first day of next month 00:00:00');
I use a crazy way to do this is using this command
$firstDay=date('Y-m-d',strtotime("first day of this month"));
$lastDay=date('Y-m-d',strtotime("last day of this month"));
Thats all
In php 5.2 you can use:
<? $d = date_create();
print date_create($d->format('Y-m-1'))->format('Y-m-d') ?>
Ugly, (and doesn't use your method call above) but works:
echo 'First day of the month: ' . date('m/d/y h:i a',(strtotime('this month',strtotime(date('m/01/y')))));
You can do it like this:
$firstday = date_create()->modify('first day January 2010');
using date method, we should be able to get the result.
ie; date('N/D/l', mktime(0, 0, 0, month, day, year));
For Example
echo date('N', mktime(0, 0, 0, 7, 1, 2017)); // will return 6
echo date('D', mktime(0, 0, 0, 7, 1, 2017)); // will return Sat
echo date('l', mktime(0, 0, 0, 7, 1, 2017)); // will return Saturday
I use this with a daily cron job to check if I should send an email on the first day of any given month to my affiliates. It's a few more lines than the other answers but solid as a rock.
//is this the first day of the month?
$date = date('Y-m-d');
$pieces = explode("-", $date);
$day = $pieces[2];
//if it's not the first day then stop
if($day != "01") {
echo "error - it's not the first of the month today";
exit;
}
Timestamp for start of this month and very last second of current month.
You can add 00:00:00 or just reference "today"
Alternative:
$startOfThisMonth = strtotime("first day of this month",strtotime("today"));
OR
$startOfThisMonth = strtotime("first day of this month 00:00:00");
$endOfThisMonth = strtotime("first day of next month",$startOfThisMonth)-1;
I am providing this answer as an alternative one liner if the DateTime object is not preferred
Basically, I get the current day number, reduce it by one then take that number of days from itself ("today" which automatically resets the clock to 00:00:00 too) and you get the start of the month.
$startOfMonth = strtotime("today - ".(date("j")-1)." days");
If you're using composer, you can install carbon:
composer require nesbot/carbon
This is then as simple as:
use Carbon/Carbon;
$startOfMonth = Carbon::now()->startOfMonth()->toDateTime();