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;
}
}
Related
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;
}
}
So I'm making a website for this pizzeria.
In the past I made a simple window that pops up Monday-Friday & Between 11.30am-02:00pm which is simple enough.
But now I've had a request to do the same thing, but instead of having a single "Lunch Buffe Ongoing!" box, the client want's a daily menu (different menu every day, which is also easy enough), but he has 3 different daily menus alternating every week.
So here's an example:
Week 1 Menu:
Monday: w1m1
Tuesday: w1m2
..
Week 2 Menu:
Monday: w2m1
Tuesday: w2m2
So my question is:
Is there a PHP function / equation for calculating "is it the first, second or third" week from a specified starting date
Edit (Not duplicate because):
This isn't a duplicate of PHP get number of week for month because I'm not asking for the week number, I'm asking what week this is alternating with 3
Answer:
<?php
function menuNumber($now) {
$then = '26/05/2014';
$first = DateTime::createFromFormat('d/m/Y', $then);
$second = DateTime::createFromFormat('d/m/Y', $now);
$calculated = (floor($first->diff($second)->days/7) % 3) + 1;
return $calculated;
}
echo "Menu Number " . menuNumber(date("d/m/Y")); // Menu Number 1
?>
Help From:
php weeks between 2 dates
PHP
<?php
function menuNumber($now) {
$then = '26/05/2014';
$first = DateTime::createFromFormat('d/m/Y', $then);
$second = DateTime::createFromFormat('d/m/Y', $now);
$calculated = (floor($first->diff($second)->days/7) % 3) + 1;
return $calculated;
}
echo "Menu Number " . menuNumber(date("d/m/Y")); // Menu Number 1
?>
Help From: PHP weeks between 2 dates
I have five files, namely index.php and first.php .. fourth.php.
first.php through fourth.php represent an annual calendar divided into 4 quarters (3 months per file). Now, I just need to display within the index page the current quarter. For example, since it's August, my index should display third.php, which contains July, August and September. In short, I need to make my index page dynamic.
This is my current code, which is obviously static:
if(!isset($_GET['quarter']) && !isset($_GET['year'])){
include ('/thirdq2012.php');
}
Also, how would I test this?
EDIT: problem fixed! i have another question, on top of every page, i have a drop down year button, which switches from 2011, 2012, 2013. my next problem is how to display the specific quarter for the selected year without creating multiple files? example quarter 1(january, february, march) of year 2011 contains data and other values that the user input, but quarter 1 of year 2012 doesnt contain anything. how will i switch on from there?
<?php
$now = new DateTime();
$month = (int)$now->format("m");
if ($month >= 1 AND $month <= 3) {
echo "First Quarter!";
}
elseif ($month >= 4 AND $month <= 6) {
echo "Second Quarter!";
}
elseif ($month >= 7 AND $month <= 9) {
echo "Third Quarter!";
}
else {
echo "Fourth Quarter!";
}
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") . "!";
I am using the following code to calculate the days remaining to make edits. They have 30 days to make edits, and days count down, this code works perfectly.
<?php
// Calculate days remains to edit or change details
$today = time();
$cdate = strtotime($row_details['payment_date']);//strtotime("19:19:09 Sep 27, 2011");
$dateDiff = $today - $cdate;
$fullDays = floor($dateDiff/(60*60*24));
$dayscalculate = 30 - $fullDays; // Set number of days
echo $dayscalculate.(($dayscalculate == 1) ? " day" : " days");
//
?>
QUESTION: if days = say 3 will say 3 days.. but if days = 0 (is the last of the 30 days).. Then want to say this is your last day or something.. So need an if based on $dayscalculate..
Ideas?
Thank you
How about...
if ($dayscalculate == 0) {
echo 'This is your last day';
} else {
printf('%d day%s',
$dayscalculate,
$dayscalculate > 1 ? 's' : ''
);
}
You may also want to introduce an "out of time" check, ie $dayscalculate < 0 but then again, you may already be handling this scenario.