Hide links after the current day - php

I'm currently building Advent calendar, and as you know, only 24 days are shown. Each of the day has a link that will shown its page.
I'm trying to show links only to the pages from day 1 to current day, not after current day.
For example, If today is 20 December, the rest (21, 22, 23, 24 December should have no link, no a href)
Here's how my code looks right now:
$date = date('Y-m-d');
echo 'Current Date: '.$date.'<br>';
for ($x = 1; $x <= 24; $x++) {
echo "<a class='grid-item' href=".'/region.php?region=Region&tag=2016-12-'.str_pad($x, 2, "0", STR_PAD_LEFT)."><em>$x</em></a><br>";
}
The output right now:
Current Date: 2016-11-11
1
2
3
4
5
..
..
24
Any suggestion would be appreciated.

Just compare $x to the day of the month. If it is greater than it, don't show the link.
$today = new DateTime();
$day = $today->format('j');
echo 'Current Date: '.$today->format('Y-m-d').'<br>';
for ($x = 1; $x <= 24; $x++) {
if ($x <= $day) {
echo "<a class='grid-item' href=".'/region.php?region=Region&tag=2016-12-'.str_pad($x, 2, "0", STR_PAD_LEFT)."><em>$x</em></a><br>";
}
else {
echo $x;
}
}

Related

PHP minus one week Sunday

I'm a newbie in both php and codeigniter.
I have a function which is used to label the x-axis of a chart of weekly sums of kilometers rode on a bicycle. It works (or is supposed to work) by counting back from today's date either zero Sundays or 1, 2, 3, etc as passed by an array.
I am using PHP Version 5.6.19, and this is my code:
In the Model:
public function week_start($date, $counter){
$tstring = strtotime($date);
$wknumber = ($counter + 1);
$start = strtotime("-$wknumber weeks sunday", $tstring);
return array(date('M d', $start));}
In the Controller:
$datenumbers = array(0, 1, 2, 3, 4, 5, 6, 7);
$dateResult = $this->bike_model->week_start(date("Y/m/d"), $datenumber);
In the view:
for ($i = 0; $i < 8; $i++){
echo "<text class = \"axis\" y = \"357\" x = \"";
echo 26 + ($i*70);
echo "\" dy = \".35em\">";
echo $dates_for_table[$i];}
I have had it up for few days and it has been showing Sunday July 3 as the first number on on my x-axis, as it should have. But today, it is Sunday and I expect it to now say Sunday July 10 as my most recent week. However, it is still showing last Sunday!
So my question is, is there a bug here or am I misunderstanding how '- one week Sunday' should work?
My goal is to have my function with zero as the argument return the most recent week start--either the current Sunday if it is Sunday or the past Sunday. I have found a workaround by adding an if else to the function checking if it is Sunday.
if ($tstring == strtotime('this Sunday')){
$wknumber = $counter;}
else{
$wknumber = ($counter + 1);}
However, this feels clunky and I'm looking for a more elegant way. I also want to understand what went wrong with "- one week Sunday" since I thought I had understood how it was working.
Thank you!
Here is how I did something similar using the object oriented DateTime style:
$startDate = new DateTime('NOW'); // can pass date string if not today
# reset start date to last Sunday or today if it is Sunday:
$startDate->sub(new DateInterval('P'.($startDate->format('w')).'D'));
# for debugging, output first Sunday:
echo $startDate->format('Y-m-d H:i:s') . " => full date and time (check for timezone issues) \n\n";
$weeks = 8;
for ($i = 0; $i < $weeks; $i++) {
# don't decrement first pass; sub 1 week on subsequent:
$startDate->sub(new DateInterval('P'. (($i) ? 1 : 0) .'W'));
echo $startDate->format('Y-m-d') . "\n";
};
You should be able to adapt for your purpose.
If you want to get fancy, you can also use DatePeriod instead of using sub within a loop:
$period = new DatePeriod(
$startDate,
DateInterval::createFromDateString('-1 week'),
7
);
foreach ($period as $date) {
echo $date->format('Y-m-d') . "\n";
}

Displaying remaining months of the year plus all months of following 2 years

I want to display all remaining months of current year followed by all months of following 2 years. Using the following code I can display next x months, but not sure how to display remaining months of current year regardless of what's the current month is, nor all months of following years.
$month = date('n');
for ($x = 0; $x < 24; $x++) {
echo date('F Y', mktime(0,0,0,$month + $x,1)) . '<br />';
}
You can use basic arithmetic: 12 - current month gives you the remaining x months of this year. Then add 24 for two following years.
$month = date('n');
$max = (12-$month) + 24;
for ($x = 0; $x < $max; $x++) {
echo date('F Y', mktime(0,0,0,$month + $x,1)) . '<br />';
}
<select name="curren" >
<?php
$mnth=date('n');
$max=13-$mnth;
?>
<?php
for($x = 0; $x < $max; $x++)
{
?>
<option value="<?php echo date("F", strtotime("+$x month"));?>"><?php echo date("F", strtotime("+$x month"));?></option>
<?php }?>
</select>

Want to show specific posts to show on specific days every week

Below is the summary :-
I have 8 different posts with a custom post type say " Class Banners "
So one of the post will always be showing on my site's home page for the upcoming fitness class for the user to see.
I have 8 Classes and so i created 8 posts this is how I want them to display :-
“Post 1” displayed Sunday 8.00am to Monday 9.30am
“Post 2” displayed Monday 9.30am to Monday 7.00pm
“Post 3” displayed Monday 7.00pm to Tuesday 6.30pm
“Post 4” displayed Tuesday 6.30pm to Wednesday 7.30pm
“Post 5” displayed Wednesday 7.30pm to Thursday 6.30pm
“Post 6” displayed Thursday 6.30pm to Friday 9.00am
“Post 7” displayed Friday 9.00am to Saturday 9.00am
“Post 8” displayed Saturday 9.00am to Sunday 8.00am
I need them to display on these specific days and time each week.
**Now what I want is :- **
I need to get those specific time queries, I know how to display each post individually.
I am just not getting it worked out to get those specific time intervals for each week so I can have 8 queries and when a specific query is met, I can show specific post my-self.
I know php, and I know where to place the queries in template but not sure how wordpress will take those queries and the right syntax that will go with wp code.
Can someone assist on this.
Thanks
<?php
$day = date("N");
$hour = date("H");
$minute = date("i";
$post1 = "Hello";
$post2 = "World";
$post3 = "I'm sorry, Dave. I'm afraid I can't let you do that.";
$post4 = "I'll be back";
$post5 = "xyz";
$post6 = "abc";
$post7 = "alphabetic";
$post8 = "numeric";
if($day == "1") // if day is monday
{
if($hour < 9)
{
echo $post8;
}
elseif($hour > 9)
{
echo $post1;
}
else
{
if($minute < 30)
{
echo $post8;
}
else
{
echo $post1;
}
}
}
... // Same for rest of week
Does this point you in the right direction??
You could try something like this:
$current = date('w') * 100 + date('H') + date('i') / 60 - 8;
$splits = array(101.5, 111, 210.5, 311.5, 410.5, 501, 601, 700);
$posts = array("Post 1", "Post 2", "Post 3", "Post 4", "Post 5", "Post 6", "Post 7", "Post 8");
$post = "Time processing error";
for ($i = 0; $i < 8; $i++) {
if ($current < $splits[$i]) {
$post = $posts[$i];
break;
}
}

display last days of previous month php

I searched the millions of similar posts about this but cannot find how to make it count down from 31, 30, 29, 28 etc.
I can have the previous calendar blocks show the 31st but that's all. I need it to show previous month 31st, 30, 29, etc.
updated code from Renku:
//define the variable dayCol
$dayCol = 0;
// Print last months' days spots.
for ($i=0; $i<$leadInDays; $i++) {
$lastmonth = date('d', strtotime(-$i.' day', strtotime($startDate))); // Days in previous month
print "<td width=\"14%\" height=\"25%\" class=\"calendar_cell_disabled_middle\">$lastmonth</td>\n ";
$dayCol++;
}
example :
I am writing a new loop for this.
<?php
$StartDate= date("Y-F-d",strtotime("+0 Month"));// get first day of current month
$num= 10; // how many past days you need from previous month + 1 (to remove current day)
for ($i=1; $i<$num; $i++) {
echo $prev= date('Y-m-d', strtotime(-$i.' day', strtotime($StartDate)))."<br />"; //get last days of previous month
}
?>
I am re writing it with your loop,
<?php
$dayCol = 0;
$leadInDays = 5; // (just for February cuz theres 5 blanks before its the 1st of Feb)
$StartDate= date("Y-F-d",strtotime("+0 Month"));
// Print last months' days spots.
for ($i=1; $i<($leadInDays+1); $i++) {
$lastmonth = date('d', strtotime(-$i.' day', strtotime($StartDate))); // Days in previous month
print "<td width=\"14%\" height=\"25%\" class=\"calendar_cell_disabled_middle\">$lastmonth</td>\n ";
$dayCol++;
}
?>
Test it Here
I would use date('t') to get the number of days in said month and just loop backwards:
$month = '2013-02-05';
for($i = date('t', strtotime($month)); $i > 0; $i--) {
...
}

PHP. How to get a bimonthly recurring event?

I'm trying many approaches but then I get stuck half way.
Let's say order was created today. I need to display when the next recurring order will happen. So I have order created June 13, 2012. Then I have set the schedule to bimonthly recurring order, every 1st of month. How to calculate when the next recurring order will happen? The answer is August 1st.
If someone can outline an approach it would be very useful, it doesn't have to be code. This is what I have so far...
// first, get starting date
$start_date_month = date('m', strtotime($start_date));
// get this year
$this_year = date('Y');
// if this month is december, next month is january
$this_month = date('m', $timestamp_month);
if($this_month == 12){
$next_month = 1;
// if this month is not december add 1 to get next month
}else{
$next_month = $this_month + 1;
}
// get array of months where recurring orders will happen
$months = array();
for ($i=1; $i<=6; $i++) {
$add_month = $start_date_month+(2*$i); // 2, 4, 6, 8, 10, 12
if($add_month == 13){$add_month = 1;$year = $this_year+1;}
elseif($add_month == 14){$add_month = 2;$year = $this_year+1;}
elseif($add_month == 15){$add_month = 3;$year = $this_year+1;}
elseif($add_month == 16){$add_month = 4;$year = $this_year+1;}
elseif($add_month == 17){$add_month = 5;$year = $this_year+1;}
elseif($add_month == 18){$add_month = 6;$year = $this_year+1;}
elseif($add_month == 19){$add_month = 7;$year = $this_year+1;}
elseif($add_month == 20){$add_month = 8;$year = $this_year+1;}
else{$year = $this_year;}
echo $what_day.'-'.$add_month.'-'.$year.'<br />';
$months[] = $add_month;
}
echo '<pre>';
print_r($months);
echo '</pre>';
I don't want to simply find what's the date in two months from now. Let's say order created June 1. Next recurring order is August 1. Then let's say now, today is September 1st, but next recurring order is October 1st. See my dilemma?
Just take the current month, so since it's June, we get 6. 6 mod 2 == 0. Next month is July, we get 7. 7 mod 2 == 1.
So just check if current month % 2 == (first month % 2).
Then just check if it's the 1st of the month.
In PHP modulus is defined with the percentage symbol.
$month = date('n');
$createdMonth = 6;
if($month % 2 == $createdMonth % 2){
// stuff
}
You might find the library called When useful for this (I'm the author).
Here is code which will get you the next 2 recurring monthly dates (from todays date):
include 'When.php';
$r = new When();
$r->recur(new DateTime(), 'monthly')
->count(2)
->interval(2) // every other month
->bymonthday(array(1));
while($result = $r->next())
{
echo $result->format('c') . '<br />';
}
// output
// 2012-08-01T13:33:33-04:00
// 2012-10-01T13:33:33-04:00
Taking this a step further, you likely only want to find the 2 first business days:
include 'When.php';
$r = new When();
$r->recur(new DateTime(), 'monthly')
->count(2)
->interval(2) // every other month
->byday(array('MO', 'TU', 'WE', 'TH', 'FR')) // week days only
->bymonthday(array(1, 2, 3)) // the first weekday will fall on one of these days
->bysetpos(array(1)); // only return one per month
while($result = $r->next())
{
echo $result->format('c') . '<br />';
}
// output
// 2012-08-01T13:33:33-04:00
// 2012-10-01T13:33:33-04:00
Also note, the code is currently under a rewrite -- it works well but it is a little confusing and not well documented.
strtotime to the rescue:
<?php
date_default_timezone_set('Europe/London');
$d = new DateTime('2012-01-31');
$d->modify('first day of +2 months');
echo $d->format('r'), "\n";
?>
Let's say you want the next six orders:
$order_date = '6/13/2012';
$start = date('Y-m-01', strtotime($order_date));
$order_count = 6;
$future_orders = array();
$next = strtotime('+2 months', strtotime($start));
while(count($future_orders) < $order_count){
$future_orders[] = date('m/d/Y',$next);
$next = strtotime('+2 months', $next);
}
This can, obviously, be improved upon, but it should get you started ...
I got this:
$today = new DateTime();
$target_date = $today->modify("first day of +2 months");
echo "Your event is on " . $target_date->format("d/m/Y") . "!";

Categories