php script working every friday - php

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');
}
}

Related

Setting time with minutes using date ();

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";
}
?>

Dynamically Change Dates for an In Between Event

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';
}

Calculate what the date will be based on startdate and value using PHP skip weekends

I have a startdate, let's say this is $startDate = 2012-08-01; and I have a variable that stores an INT value, lets say this is $value = 10;
I would like to calculate what the date would be from startdate + 10 days and skip weekends.
Using the above values the result would be 2012-08-15
How would this be done?
This is far from efficient, but who cares about that right when it is readable? :)
<?php
function calculateNextDate($startDate, $days)
{
$dateTime = new DateTime($startDate);
while($days) {
$dateTime->add(new DateInterval('P1D'));
if ($dateTime->format('N') < 6) {
$days--;
}
}
return $dateTime->format('Y-m-d');
}
echo calculateNextDate('2012-08-01', 10); // return 2012-08-15
DEMO
What happens should be pretty easy to follow. First we create a new DateTime object using the date provided by the user. After that we are looping through the days we want to add to the date. When we hit a day in the weekend we don't subtract a day from the days we want to add to the date.
you can use php's strtotime function to + n days/hours etc..,
and for excluding weekends have a look here:
32 hours ago excluding weekends with php
Try this
<?php
function businessdays($begin, $end) {
$rbegin = is_string($begin) ? strtotime(strval($begin)) : $begin;
$rend = is_string($end) ? strtotime(strval($end)) : $end;
if ($rbegin < 0 || $rend < 0)
return 0;
$begin = workday($rbegin, TRUE);
$end = workday($rend, FALSE);
if ($end < $begin) {
$end = $begin;
$begin = $end;
}
$difftime = $end - $begin;
$diffdays = floor($difftime / (24 * 60 * 60)) + 1;
if ($diffdays < 7) {
$abegin = getdate($rbegin);
$aend = getdate($rend);
if ($diffdays == 1 && ($astart['wday'] == 0 || $astart['wday'] == 6) && ($aend['wday'] == 0 || $aend['wday'] == 6))
return 0;
$abegin = getdate($begin);
$aend = getdate($end);
$weekends = ($aend['wday'] < $abegin['wday']) ? 1 : 0;
} else
$weekends = floor($diffdays / 7);
return $diffdays - ($weekends * 2);
}
function workday($date, $begindate = TRUE) {
$adate = getdate($date);
$day = 24 * 60 * 60;
if ($adate['wday'] == 0) // Sunday
$date += $begindate ? $day : -($day * 2);
return $date;
}
$def_date="";//define your date here
$addDay='5 days';//no of previous days
date_add($date, date_interval_create_from_date_string($addDay));
echo businessdays($date, $def_date); //date prior to another date
?>
Modified from PHP.net
if you just want to add up a date +10, you may wanna consider this:
date("Y-m-d", strtotime("+10 days"));

PHP redirect on exact date

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

Add working hours to timestamp

I need to add working hours to a timestamp. Working hours are from 8am to 6pm. Lets say we have 2pm and I have to add 6 hours. Result should be 10am... any guesses?
Thanks.
Try this bad boy.
You can specify whether to include weekends as working days, etc. Doesn't take into account holidays.
<?php
function addWorkingHours($timestamp, $hoursToAdd, $skipWeekends = false)
{
// Set constants
$dayStart = 8;
$dayEnd = 16;
// For every hour to add
for($i = 0; $i < $hoursToAdd; $i++)
{
// Add the hour
$timestamp += 3600;
// If the time is between 1800 and 0800
if ((date('G', $timestamp) >= $dayEnd && date('i', $timestamp) >= 0 && date('s', $timestamp) > 0) || (date('G', $timestamp) < $dayStart))
{
// If on an evening
if (date('G', $timestamp) >= $dayEnd)
{
// Skip to following morning at 08XX
$timestamp += 3600 * ((24 - date('G', $timestamp)) + $dayStart);
}
// If on a morning
else
{
// Skip forward to 08XX
$timestamp += 3600 * ($dayStart - date('G', $timestamp));
}
}
// If the time is on a weekend
if ($skipWeekends && (date('N', $timestamp) == 6 || date('N', $timestamp) == 7))
{
// Skip to Monday
$timestamp += 3600 * (24 * (8 - date('N', $timestamp)));
}
}
// Return
return $timestamp;
}
// Usage
$timestamp = time();
$timestamp = addWorkingHours($timestamp, 6);
A more compact version:
function addWhours($timestamp, $hours, $skipwe=false, $startDay='8', $endDay='18')
{
$notWorkingInterval = 3600 * (24 - ($endDay - $startDay));
$timestamp += 3600*$hours;
$our = date('H', $timestamp);
while ($our < $startDay && $our >= $endDay) {
$timestamp += $notWorkingInterval;
$our = date('H', $timestamp);
}
$day = date('N', $timestamp);
if ($skipwe && $day >5) {
$timestamp += (8-$day)*3600*24;
}
return $timestamp;
}
If it is a real timestamp, you just need to add the seconds equivelent to 6 hours.
$timestamp += 3600 * 6;
If not we need to know the real format of your "timestamp".

Categories