I'm using this code to redirect based on the hour of the day, and the day of the week.
Here it is ..
<?php
$hour = date('G');
$minute = date('i');
$day = date('w');
$m = $hour * 60 + $minute; // Minutes since midnight.
if (
$day == 0 // Sunday...
&& $m >= 615 // ... after 10:15…
&& $m <= 700 // ... but before 11:40…
) {
header("Location: open.php");
}
else
if (
$day == 3 // Wednesday...
&& $m >= 1125 // ... after 18:45…
&& $m <= 1235 // ... but before 20:35…
) {
header("Location: open.php");
}
?>
I was wondering if there was a way to redirect to a page based on an exact date in the future like April 25th or November 1st.
Thanks .
This is a simple approach, which does not take account the year date:
// 25th april:
if (date('d') == '25' && date('m') == '4') {
header('Location: open.php');
}
// 1st nov:
if (date('d') == '1' && date('m') == '11') {
header('Location: open.php');
}
Look at the date() documentation for more exact details.
Convert the redirection date into timestamp using strtotime() or date() and you can do it easily
if(strtotime("2012/5/3") <= time()) {
header("location: toredirect.php");
exit;
}
Sure.
$date = date();
$redirectDate = Date here;
if($date == $redirectDate) {
header("Location: open.php");
}
This is going to be based on your server date/time
Related
So I have this code that works perfectly:
<?php
$time = date("H");
$timezone = date("e");
if ($time < "13") {
echo "message1";
} else
if ($time >= "13" && $time < "19") {
echo "message2";
} else
if ($time >= "19" && $time < "24") {
echo "message3";
}
?>
The problem is that I can only use hours: 1, 5, 10, 19 and etc.
I need to use minutes as well, eg. 13:30.
I have tried changing to 13:30. Also tried changing to 1350.
But can't make it work.
Need help.
Create a new variable that hold the current minute,
DateTime::format shows us that date("i") gives:
Minutes with leading zeros
With the current hour and minute, you can calculate any time, for example:
<?php
$hour = date("H");
$minute = date("i");
// Between 13:30 and 13:30
if ($hour === 13 && $minute <= 30) {
echo "message1";
}
// Between 13:30 and 19:00
elseif (($hour >= 13 && $minute >= 30) && $hour < 19) {
echo "message2";
}
// After 19:00
else {
echo "message3";
}
?>
Based on a piece of coding from Rounin - I have taken this to try and come up with a method of, basically if todays date is between say 2017-03-01 and 2017-04-30, then display rego form. If outside of these dates, then display "Form Not available"
The date in between is two set dates is easy to understand, but what I want to do is dynamically change the Y so I don't have to edit manually every year.
What I have is (although seems very unwieldy - but works?)
<?php
$today = date('Y-m-d');
$Current_Year = date('y');
$Current_Month = date('n');
$Current_Day = date('j');
$Next_Year = ($Current_Year + 1);
if ($Current_Month < 5) {
$Begin_Rego_Year = $Current_Year;
}
if ($Current_Month > 3) {
$Begin_Rego_Year = $Next_Year;
}
# Checks if date is March 31st
if (($Current_Month == 3) && ($Current_Day == 1)) {
$Begin_Rego_Year = $Current_Year;
}
$Begin_Rego_Date = $Begin_Rego_Year.'-03-01';
/////////////////
if ($Current_Month < 5) {
$End_Rego_Year = $Current_Year;
}
if ($Current_Month > 3) {
$End_Rego_Year = $Next_Year;
}
# Checks if date is March 31st
if (($Current_Month == 4) && ($Current_Day == 30)) {
$End_Rego_Year = $Current_Year;
}
$End_Rego_Date = $End_Rego_Year.'-04-30';
if (($today >= $Begin_Rego_Date) && ($today <= $End_Rego_Date))
{
echo "Display Rego Form"; //if todays date was between 01 Mar - 30 Apr 17
}
else {
echo "Rego Form Offline until 01 Mar 18";
}
?>
Easist way to work with dates i think it is a function strtotime
#usage example strtotime();
if (
strtotime('today') >= strtotime('first day of this month')
&& strtotime('today') <= strtotime('last day of this month')
) {
echo 'display rego form';
}
php.net
Info about this function
Info about date formats for strtotime
Not sure if this is legal.. strtotime('last day of april') - couldn't make it work.
Ended up with (so far)
$open_day = date('01-03-Y');
$close_day = date('30-04-Y');
if (
strtotime('today') >= strtotime($open_day) && strtotime('today') <= strtotime($close_day)
) {
echo 'display rego form';
}
else {
echo 'Come back another time';
}
I'm trying to make a script to display specific messages at specific day and time. I've got the script working to display at a specific day and at a specific time, but not the two combined.
How can I make the script display a specific message at mondays between 3pm and 4pm?
Look at the script I made so far;
<?php
//Messages
$afternoon = "It's afternoon";
$evening = "It's evening";
$night = "It's night.";
$morning = "It's morning!";
$friday = "Soon weekend!";
//Get time
$current_time = date(G);
//Get day
$current_day = date(l);
//12PM - 4PM
if ($current_time >= 12 && $current_time <= 16) {
echo $afternoon;
}
//5PM - 11PM
elseif ($current_time >= 17 && $current_time <= 24) {
echo $evening;
}
//00AM - 05AM
elseif ($current_time >= 1 && $current_time <= 5) {
echo $night;
}
//6AM - 11AM
elseif ($current_time >= 6 && $current_time <= 11) {
echo $morning;
}
//Friday, display message
if ($current_day == "Friday") {
echo $friday;
}
?>
I appreciate any help I can get!
if ($current_day == "Monday") {
if ($current_time >= 15 && $current_time <= 16) {
echo "It's Monday and the time is between 3PM and 4PM.";
}
}
if($current_day == "Monday" && $current_time >= 3 && $current_time <= 4){
//Monday text
}
Try this:
Set the flag true when its afternoon and later if the day is monday set the message as needed.
$afternoonFlag = False;
if ($current_time >= 12 && $current_time <= 16) {
echo $afternoon;
$afternoonFlag = True;
}
if ($current_day == "Monday" && $afternoonFlag) {
echo "Your Message";
}
I have this script that is working every day between 2 hours. Now, I have 2 questions:
1) how do I make it work only every friday, not all days?
2) how do I use minutes to m code? $hour:$min > 8:15 is not working
<?php
$hour = date('G');
if ($hour >= 8 && $hour <= 10) {
include('facut_mine2.php');
}
?>
Thanks!
use date('w') to get weekday
<?php
$start = strtotime('8:15');
$end = strtotime('9:45');
if(date('w') == 5){ // day 5 = Friday
$timenow = date('U');
if ($timenow >= $start && $timenow <= $end) {
include('facut_mine2.php');
}
}
?>
<?php
$hour = date('G');
$day = date('w');
if ($day == 5 && $hour >= 8 && $hour <= 10) {
include('facut_mine2.php');
}
?>
by using D parameter in date() function you can retrieve the current week day:
$today = date('D');// $today = 'Wed'
and for minute you can again use date() function with i
$current_min = date('i');
if(date('D') == 'Fri') {
$hourMin = date('H:i');
if ($hourMin >= '08:15' && $hourMin <= '10:00') {
include('facut_mine2.php');
}
}
Can anyone extrapolate on how to implement a basic "good evening" or "good morning" based on the user's time setting?
Perhaps PHP will fetch the server time, but I'm looking to greet the site visitor with a time-based appropriate greeting that considers their time of day.
E.G.: good morning, good night, good afternoon.
Base it on .getHours() of the date object. Using javascript's Date object will automatically use the user's local time, rather than the server-time:
var now = new Date();
alert( now.getHours() );
A couple conditional checks, and you're in business. For instance, the following is a very simple and easy-to-understand example:
var now = new Date();
var hrs = now.getHours();
var msg = "";
if (hrs > 0) msg = "Mornin' Sunshine!"; // REALLY early
if (hrs > 6) msg = "Good morning"; // After 6am
if (hrs > 12) msg = "Good afternoon"; // After 12pm
if (hrs > 17) msg = "Good evening"; // After 5pm
if (hrs > 22) msg = "Go to bed!"; // After 10pm
alert(msg);
It's currently 2:56am here, so I see "Mornin' Sunshine!" when I run this. You can test your own local time with this online demo: http://jsbin.com/aguyo3/edit
<?php
// I'm India so my timezone is Asia/Calcutta
date_default_timezone_set('Asia/Calcutta');
// 24-hour format of an hour without leading zeros (0 through 23)
$Hour = date('G');
if ( $Hour >= 5 && $Hour <= 11 ) {
echo "Good Morning";
} else if ( $Hour >= 12 && $Hour <= 18 ) {
echo "Good Afternoon";
} else if ( $Hour >= 19 || $Hour <= 4 ) {
echo "Good Evening";
}
?>
For more information about date see function.date. I Hope this will help.
<?php
/* This sets the $time variable to the current hour in the 24 hour clock format */
$time = date("H");
/* Set the $timezone variable to become the current timezone */
$timezone = date("e");
/* If the time is less than 1200 hours, show good morning */
if ($time < "12") {
echo "Good morning";
} else
/* If the time is grater than or equal to 1200 hours, but less than 1700 hours, so good afternoon */
if ($time >= "12" && $time < "17") {
echo "Good afternoon";
} else
/* Should the time be between or equal to 1700 and 1900 hours, show good evening */
if ($time >= "17" && $time < "19") {
echo "Good evening";
} else
/* Finally, show good night if the time is greater than or equal to 1900 hours */
if ($time >= "19") {
echo "Good night";
}
?>
$hour = date('H');
if ($hour >= 20) {
$greetings = "Good Night";
} elseif ($hour > 17) {
$greetings = "Good Evening";
} elseif ($hour > 11) {
$greetings = "Good Afternoon";
} elseif ($hour < 12) {
$greetings = "Good Morning";
}
echo $greetings;
const now = new Date().getHours();
switch (true) {
case (now<=12): console.log("Good Morning");break;
case (now>12 && now<16): console.log("Good Afternnon");break;
case (now>=16 && now<20): console.log("Good Evening");break;
case (now>=20 && now<=24): console.log("Good Night");break;}
This code is based on JavaScript, Hope this will help you.
function greeting_msg() {
$hour = date('H');
if ($hour >= 18) {
$greeting = "Good Evening";
} elseif ($hour >= 12) {
$greeting = "Good Afternoon";
} elseif ($hour < 12) {
$greeting = "Good Morning";
}
return $greeting;
}