PHP switch: calculate dates - php

I made this switch, but I dont know this last step (the ?? and the comments).
If someone can help me, thanks a lot!
<?
$nuDatumTijd = date("Y-m-d H:i:s");
$nuUur = date("H");
switch(true)
{
case $nuUur > 8 && $nuUur < 13:
$aantSecErbij = ??; // number of seconds untill the first next 13:00
$weerOp = date($nuDatumTijd,$aantSecErbij);
echo $weerOp;
break;
case $nuUur > 12 && $nuUur < 18:
$aantSecErbij = ??; // number of seconds untill the first next 19:00
$weerOp = date($nuDatumTijd,$aantSecErbij);
echo $weerOp;
break;
case $nuUur > 17 && $nuUur < 22:
$aantSecErbij = ??; // number of seconds untill the first next 9:00 (so that is the next day)
$weerOp = date($nuDatumTijd,$aantSecErbij);
echo $weerOp;
break;
}
?>

I now have fixed it this way, thanks for your input:
$nuUur = date("H");
if ($nuUur > 8 && $nuUur < 13)
{
$weerOp = strtotime('today 13:00');
$weerOpNetjes = date('Y-m-d H:i:s', $weerOp);
echo $weerOpNetjes;
}
elseif ($nuUur > 12 && $nuUur < 18)
{
$weerOp = strtotime('today 19:00');
$weerOpNetjes = date('H:i', $weerOp);
echo $weerOpNetjes;
}
elseif ($nuUur > 17 && $nuUur < 22)
{
$weerOp = strtotime('tomorrow 9:00');
$weerOpNetjes = date('H:i', $weerOp);
echo $weerOpNetjes;
}

switch-case can be used this way too, no problem about that.
$aantSecErbij = ??; // number of seconds untill the first next 13:00
If this means the number of seconds until today's 1pm then the following will get you the timestamp of today's 1pm.
$now = getdate();
$time1pm = mktime(13, 0, 0, $now['mon'], $now['mday'], $now['year']);
If it means the number of seconds between now and today's 1pm then you need to subtract with current timestamp.
$now = getdate();
$diff1pm = mktime(13, 0, 0, $now['mon'], $now['mday'], $now['year']) - time();
.
$aantSecErbij = ??; // number of seconds untill the first next 9:00 (so that is the next day)
For the next day you can add 1 to the day part and don't have to worry about the end of month. PHP will figure out the right date for you.
$now = getdate();
$tomorrow9am = mktime(9, 0, 0, $now['mon'], $now['mday'] + 1, $now['year']);

Related

How to create a php array for a month to display an image

My dad wrote this code for me a long time ago. The code lives on my computer and then fetches an online image based on the holiday and inserts it into my HTML email signature. If there is no specific holiday, it defaults to the image for the season. I can figure out specific days to be added. But, I don't understand how to add an entry for an entire month.
If I wanted to say make a Black History month that lasted an entire month, how would I do that?
Thanks for any help. There are probably better ways to do this now, but I feel kinda sentimental about this code.
Thanks for any direction.
(:
#!/usr/local/bin/php -q
<?php
// Modified March 25, 2017
// This function return the season as text
date_default_timezone_set('America/New_York');
$unixTimeStamp = time();
$jd = date('z',$unixTimeStamp);
if ($jd == 0) $jd =1;
$current_year = date('Y',$unixTimeStamp);
//setup array with the day of November for Thanksgiving starting in 2017
// additional years can be added after 2035.
// similar setup for equinox and solstice below
// Information for dates can be found in www.timeanddate.com
$thanksgiving_day = array(2017 => 24,23,22,28,26,25,24,23,28,27,26,25,23,22,28,27,25,24,23,22); // good til 2035
$independence_day = array(2017 => 4,4,4,3,4,5,4,4,4,4,3,5,4,4,4,4,5,4,4,4); // good til 2035
$vernal_equinox_date = array(2017 => 20,20,20,19,20,20,20,19,20,20,20,19,20,20,20,19,20,20,20); // good til 2035
$summer_solstice_date = array(2017 => 20,21,21,20,20,21,21,20,20,20,21,20,20,21,21,20,20,21,21); // good til 2035
$autumal_equinox_date = array(2017 => 22,22,23,22,22,22,23,22,22,22,23,22,22,22,23,22,22,22,23); // good til 2035
$veterans_day = array(2017 => 10,12,11,11,11,11,10,11,11,11,11,10,12,11,11,11,11,10,12); // good til 2035
$winter_solstice_date = 21; // does not change until 2044
$easter_day = array(2017 => 16,1,21,12,4,17,9,31,20,5,28,16,1,21,13,28,17,9,25); //good through 2035. Date information from Wikipedia
$easter_month = array(2017 => 4,4,4,4,4,4,4,3,4,4,3,4,4,4,4,34,4,3); //good through 2035
// find the day of the year for each special holiday
// notice: mktime(0,0,0,month, day, year)
// I may be able to calculate holidays such as Thanksgiving,
// but holidays such as halloween are easy. Just follow the schema
// below.
// then add a new case statement above the top case statement.
// use christmas for the example.
// See Christmas line:
// case $jd >= $christmas - 3 && $jd <= $christmas + 3:
// From 3 days before christmas until 3 days after christmas.
// What is actually says is if today is less than christmas minus 3
// and greater than christmas plus 3.
// PERMISSIONS: These will probably need to be changed if you replace the script:
// Open terminal and type cd ~/bin
// Type: chmod 777 _seasons.php
$valentines = date('z', mktime(0,0,0,2,14,$current_year));
$spring = date('z', mktime(0,0,0,3,$vernal_equinox_date[$current_year],$current_year));
$easter = date('z', mktime(0,0,0,$easter_month[$current_year],$easter_day[$current_year],$current_year));
$summer = date('z', mktime(0,0,0,6,$vernal_equinox_date[$current_year],$current_year));
$independenceday = date('z', mktime(0,0,0,7,$independence_day[$current_year], $current_year));
$fall = date('z', mktime(0,0,0,9,$autumal_equinox_date[$current_year],$current_year));
$navyday = date('z', mktime(0,0,0,10,13,$current_year));
$halloween = date('z', mktime(0,0,0,10,31,$current_year));
$veteransday = date('z', mktime(0,0,0,11, $veterans_day[$current_year], $current_year));
$thanksgiving = date('z', mktime(0,0,0,11,$thanksgiving_day[$current_year],$current_year));
$winter = date('z', mktime(0,0,0,12,21,$current_year));
$christmas = date('z', mktime(0,0,0,12,25,$current_year));
$newyears = date('z', mktime(0,0,0,1,1,$current_year));
switch ($jd) {
case $jd >= $christmas - 14 && $jd <= $christmas + 3:
$sp = "christmas";
break;
case $jd >= $newyears && $jd <= $newyears + 5:
$sp = "newyears";
break;
case $jd >= $halloween - 7 && $jd <= $halloween + 0:
$sp = "halloween";
break;
case $jd == $veteransday:
$sp = "veteransday";
break;
case $jd >= $thanksgiving - 5 && $jd <= $thanksgiving + 0:
$sp = "thanksgiving";
break;
case $jd == $navyday:
$sp = "navyday";
break;
case $jd == $independenceday:
$sp = "independenceday";
break;
case $jd == $valentines:
$sp = "valentines";
break;
case $jd >= $easter - 5 && $jd <= $easter + 0:
$sp = "easter";
break;
case $jd < $spring || $jd >= $winter:
$sp = "winter";
break;
case $jd < $summer:
$sp = "spring";
break;
case $jd < $fall:
$sp = "summer";
break;
case $jd < $winter:
$sp = "fall";
break;
}
echo "<img src=\"http://www.website.com/stamps/Web-Logo-stamp-$sp.jpg\" alt=\"Web Site Title\" width=\"162\" height=\"125\" border=\"0\" style=\"float: left; padding: 0px 16px 0 0;\">";
?>
Calculate the start of Black History Month:
$blackhistory = date('z', mktime(0, 0, 0, 2, 1, $current_year));
and in your switch statement:
case $jd >= $blackhistory && $jd <= $blackhistory + 28 + date('L'):
$sp = "blackhistory";
break;
February is normally 28 days, date('L') is 1 when it's a leap year, so adding it will correct for leap years.

Pulling in data around/after midnight

Ok.. so I've created myself a messy problem.
So I have a jquery slider that shows 5 slides of content
1) -2 hours ago
2) -1 hour ago
3) (0) present
4) +1 hours
5) +2 hours
The content for each slide is decided by what time of day it is. However when it comes to trying to go back/forwards 1 to 2 hours during the run up to midnight and after midnight the whole script breaks, and kills the site.
<?php
$h = date('G', strtotime ("-2 hour")); //set variable $h to the hour of the day.
$m = date('i', strtotime ("-2 hour")); //set variable $m to the min of the hour.
$d = date('w', strtotime ("-2 hour")); //set variable $d to the day of the week.
// SATURDAY SCHEDULE
if ($d == 6 && $h >= 0 && $h < 07) $file ='earlyhours.php';
else if ($d == 6 && $h >= 07 && $h < 09) $file ='breakfast.php';
else if ($d == 6 && $h >= 09 && $h < 12) $file ='throughthemorning.php';
else if ($d == 6 && $h >= 12 && $h < 13) $file ='rewind.php';
else if ($d == 6 && $h >= 13 && $h < 17 && $m <= 30) $file ='nonstop.php';
else if ($d == 6 && $h >= 17 && $m >= 30 && $h <21) $file ='livetrend.php';
else if ($d == 6 && $h >= 17 && $m >= 35 && $h < 21) $file ='nonstop.php';
else if ($d == 6 && $h >= 21 && $h < 18) $file ='gennation.php';
else if ($d == 6 && $h >= 23) $file ='earlyhours.php';
else if ($d == 7 && $h >= 0) $file ='earlyhours.php';
require $_SERVER['DOCUMENT_ROOT'] . '/collection/profiles/' . $file . '';
?>
As you can see it figures the time and then drops the correct file in - there's five of these for everyday of the week (-2.php, -1.php, 0.php, 1.php, 2.php).
Does anybody have a solution? Ideally I need to stop the break, but I don't want my visitors to be scrolling +1 / 2 or -1 / 2 hours on for the same days rotation when it nears, and steps over midnight.
For example, right now the code is broken on -2.php until at least 2am (my timezone) so that it back track.
I've totally burnt myself out trying to figure this one out.
The problems arising from the change of day can become intractable. I'd tackle this a different way. Start by calculating the day and hour for the five periods your interested in and use DateTime to do the heavy lifting. Then, use a function to provide the schedule item for a particular day/time combination.
Here's a skeleton
<?php
date_default_timezone_set('Pacific/Auckland');
$now = new DateTime();
$oneHour = new DateInterval('PT1H');
$minusOne = (clone $now);
$minusOne->sub($oneHour);
$minusTwo = (clone $minusOne);
$minusTwo->sub($oneHour);
$plusOne = (clone $now);
$plusOne->add($oneHour);
$plusTwo = (clone $plusOne);
$plusTwo->add($oneHour);
echo returnFile($minusTwo);
echo returnFile($minusOne);
echo returnFile($now);
echo returnFile($plusOne);
echo returnFile($plusTwo);
function returnFile(DateTime $t) {
$day = $t->format('D');
$hour = $t->format('G');
// echo "Day:$day, Hour: $hour;<br>";
switch ($day) {
case 'Mon':
if ($hour<7) {
// Small hours Monday...
$filename = "smallMonday.html";
break;
}
if ($hour<12) {
// Monday morning
$filename = "morningMonday.html";
break;
}
break;
case 'Tue':
if ($hour >=23) {
// Late Tuesday
$filename = "lateTuesday.html";
}
default:
$filename = "Some other time";
}
return $filename;
}
?>
I haven't put in a complete schedule - you can work that out.
If you're using PHP 5.5 or later you can use DateTimeImmutable instead of DateTime which does away with all the cloning.
There's a fiddle here
Get rid of the leading zeros in your comparisons. Those are octal numbers and not decimals. You won't get the results you expect.
// 09 is not a valid octal number. It gets converted to zero in decimal.
else if ($d == 6 && $h >= 07 && $h < 09)
..
else if ($d == 6 && $h >= 09 && $h < 12) $file ='throughthemorning.php';

Get time before noon

I am practicing with dates in php. I a bit of a newbie so bear my ignorance
I am trying to see when a time is before noon.
So I have a variable coming in with this format 2014-03-07 13:28:00.000
I get the time like this
$submissonTime = date('H:i:s', strtotime($value['job_submission_date']));
then I want to set another variable as $noon and i am doing this:
$noon = date('H:i:s', '12:00:00.000');
However the value of noon is 12:00:12
what i want to do is basically:
if($submissionTime <= $noon){
//do my stuff
}
NB I want to enter the if statement when even when it is 12:00:00 and stop entering when it is 12:00:01
Any help please?
Try
$noon = date('Y-m-d 12:00:00'); // today noon with date
$submissonTime = date('Y-m-d H:i:s', strtotime($value['job_submission_date']));
if(strtotime($submissonTime) <= strtotime($noon)){
//do my stuff
}
if you want to compare only time use both format
$noon = date('12:00:00');
$submissonTime = date('H:i:s', strtotime($value['job_submission_date']));
if (date("A") == "AM")
{
// AM-Code
} else {
// PM-Code
}
Why don't you go with only one string of code getting the hour?
$Hour = date("G"); //24-hour format of an hour without leading zeros
if($Hour < 12) {
// do the code
}
Or in your case
$Hour = date("G", strtotime($value['job_submission_date']));
update
If you need 12:00:00 and not 12:00:01 and later on, you will need to define minutes and seconds:
$Hour = date("G"); //24-hour format of an hour without leading zeros
$Minute = intval(date("i")); // will give minutes without leading zeroes
$Second = intval(date("s"));
if(($Hour < 12) || ($Hour == 12 && $Minute == 0 && Second == 0)) {
// do the code
}

Get week number (in the year) from a date PHP

I want to take a date and work out its week number.
So far, I have the following. It is returning 24 when it should be 42.
<?php
$ddate = "2012-10-18";
$duedt = explode("-",$ddate);
$date = mktime(0, 0, 0, $duedt[2], $duedt[1],$duedt[0]);
$week = (int)date('W', $date);
echo "Weeknummer: ".$week;
?>
Is it wrong and a coincidence that the digits are reversed? Or am I nearly there?
Today, using PHP's DateTime objects is better:
<?php
$ddate = "2012-10-18";
$date = new DateTime($ddate);
$week = $date->format("W");
echo "Weeknummer: $week";
It's because in mktime(), it goes like this:
mktime(hour, minute, second, month, day, year);
Hence, your order is wrong.
<?php
$ddate = "2012-10-18";
$duedt = explode("-", $ddate);
$date = mktime(0, 0, 0, $duedt[1], $duedt[2], $duedt[0]);
$week = (int)date('W', $date);
echo "Weeknummer: " . $week;
?>
$date_string = "2012-10-18";
echo "Weeknummer: " . date("W", strtotime($date_string));
Use PHP's date function
http://php.net/manual/en/function.date.php
date("W", $yourdate)
This get today date then tell the week number for the week
<?php
$date=date("W");
echo $date." Week Number";
?>
Just as a suggestion:
<?php echo date("W", strtotime("2012-10-18")); ?>
Might be a little simpler than all that lot.
Other things you could do:
<?php echo date("Weeknumber: W", strtotime("2012-10-18 01:00:00")); ?>
<?php echo date("Weeknumber: W", strtotime($MY_DATE)); ?>
Becomes more difficult when you need year and week.
Try to find out which week is 01.01.2017.
(It is the 52nd week of 2016, which is from Mon 26.12.2016 - Sun 01.01.2017).
After a longer search I found
strftime('%G-%V',strtotime("2017-01-01"))
Result: 2016-52
https://www.php.net/manual/de/function.strftime.php
ISO-8601:1988 week number of the given year, starting with the first week of the year with at least 4 weekdays, with Monday being the start of the week. (01 through 53)
The equivalent in mysql is DATE_FORMAT(date, '%x-%v')
https://www.w3schools.com/sql/func_mysql_date_format.asp
Week where Monday is the first day of the week (01 to 53).
Could not find a corresponding solution with DateTime.
At least not without solutions like "+1day, last monday".
Edit: since strftime is now deprecated, maybe you can also use date.
Didn't verify it though.
date('o-W',strtotime("2017-01-01"));
I have tried to solve this question for years now, I thought I found a shorter solution but had to come back again to the long story. This function gives back the right ISO week notation:
/**
* calcweek("2018-12-31") => 1901
* This function calculates the production weeknumber according to the start on
* monday and with at least 4 days in the new year. Given that the $date has
* the following format Y-m-d then the outcome is and integer.
*
* #author M.S.B. Bachus
*
* #param date-notation PHP "Y-m-d" showing the data as yyyy-mm-dd
* #return integer
**/
function calcweek($date) {
// 1. Convert input to $year, $month, $day
$dateset = strtotime($date);
$year = date("Y", $dateset);
$month = date("m", $dateset);
$day = date("d", $dateset);
$referenceday = getdate(mktime(0,0,0, $month, $day, $year));
$jan1day = getdate(mktime(0,0,0,1,1,$referenceday[year]));
// 2. check if $year is a leapyear
if ( ($year%4==0 && $year%100!=0) || $year%400==0) {
$leapyear = true;
} else {
$leapyear = false;
}
// 3. check if $year-1 is a leapyear
if ( (($year-1)%4==0 && ($year-1)%100!=0) || ($year-1)%400==0 ) {
$leapyearprev = true;
} else {
$leapyearprev = false;
}
// 4. find the dayofyearnumber for y m d
$mnth = array(0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334);
$dayofyearnumber = $day + $mnth[$month-1];
if ( $leapyear && $month > 2 ) { $dayofyearnumber++; }
// 5. find the jan1weekday for y (monday=1, sunday=7)
$yy = ($year-1)%100;
$c = ($year-1) - $yy;
$g = $yy + intval($yy/4);
$jan1weekday = 1+((((intval($c/100)%4)*5)+$g)%7);
// 6. find the weekday for y m d
$h = $dayofyearnumber + ($jan1weekday-1);
$weekday = 1+(($h-1)%7);
// 7. find if y m d falls in yearnumber y-1, weeknumber 52 or 53
$foundweeknum = false;
if ( $dayofyearnumber <= (8-$jan1weekday) && $jan1weekday > 4 ) {
$yearnumber = $year - 1;
if ( $jan1weekday = 5 || ( $jan1weekday = 6 && $leapyearprev )) {
$weeknumber = 53;
} else {
$weeknumber = 52;
}
$foundweeknum = true;
} else {
$yearnumber = $year;
}
// 8. find if y m d falls in yearnumber y+1, weeknumber 1
if ( $yearnumber == $year && !$foundweeknum) {
if ( $leapyear ) {
$i = 366;
} else {
$i = 365;
}
if ( ($i - $dayofyearnumber) < (4 - $weekday) ) {
$yearnumber = $year + 1;
$weeknumber = 1;
$foundweeknum = true;
}
}
// 9. find if y m d falls in yearnumber y, weeknumber 1 through 53
if ( $yearnumber == $year && !$foundweeknum ) {
$j = $dayofyearnumber + (7 - $weekday) + ($jan1weekday - 1);
$weeknumber = intval( $j/7 );
if ( $jan1weekday > 4 ) { $weeknumber--; }
}
// 10. output iso week number (YYWW)
return ($yearnumber-2000)*100+$weeknumber;
}
I found out that my short solution missed the 2018-12-31 as it gave back 1801 instead of 1901. So I had to put in this long version which is correct.
How about using the IntlGregorianCalendar class?
Requirements: Before you start to use IntlGregorianCalendar make sure that libicu or pecl/intl is installed on the Server.
So run on the CLI:
php -m
If you see intl in the [PHP Modules] list, then you can use IntlGregorianCalendar.
DateTime vs IntlGregorianCalendar:
IntlGregorianCalendar is not better then DateTime. But the good thing about IntlGregorianCalendar is that it will give you the week number as an int.
Example:
$dateTime = new DateTime('21-09-2020 09:00:00');
echo $dateTime->format("W"); // string '39'
$intlCalendar = IntlCalendar::fromDateTime ('21-09-2020 09:00:00');
echo $intlCalendar->get(IntlCalendar::FIELD_WEEK_OF_YEAR); // integer 39
<?php
$ddate = "2012-10-18";
$duedt = explode("-",$ddate);
$date = mktime(0, 0, 0, $duedt[1], $duedt[2],$duedt[0]);
$week = (int)date('W', $date);
echo "Weeknummer: ".$week;
?>
You had the params to mktime wrong - needs to be Month/Day/Year, not Day/Month/Year
To get the week number for a date in North America I do like this:
function week_number($n)
{
$w = date('w', $n);
return 1 + date('z', $n + (6 - $w) * 24 * 3600) / 7;
}
$n = strtotime('2022-12-27');
printf("%s: %d\n", date('D Y-m-d', $n), week_number($n));
and get:
Tue 2022-12-27: 53
for get week number in jalai calendar you can use this:
$weeknumber = date("W"); //number week in year
$dayweek = date("w"); //number day in week
if ($dayweek == "6")
{
$weeknumberint = (int)$weeknumber;
$date2int++;
$weeknumber = (string)$date2int;
}
echo $date2;
result:
15
week number change in saturday
The most of the above given examples create a problem when a year has 53 weeks (like 2020). So every fourth year you will experience a week difference. This code does not:
$thisYear = "2020";
$thisDate = "2020-04-24"; //or any other custom date
$weeknr = date("W", strtotime($thisDate)); //when you want the weeknumber of a specific week, or just enter the weeknumber yourself
$tempDatum = new DateTime();
$tempDatum->setISODate($thisYear, $weeknr);
$tempDatum_start = $tempDatum->format('Y-m-d');
$tempDatum->setISODate($thisYear, $weeknr, 7);
$tempDatum_end = $tempDatum->format('Y-m-d');
echo $tempDatum_start //will output the date of monday
echo $tempDatum_end // will output the date of sunday
Very simple
Just one line:
<?php $date=date("W"); echo "Week " . $date; ?>"
You can also, for example like I needed for a graph, subtract to get the previous week like:
<?php $date=date("W"); echo "Week " . ($date - 1); ?>
Your code will work but you need to flip the 4th and the 5th argument.
I would do it this way
$date_string = "2012-10-18";
$date_int = strtotime($date_string);
$date_date = date($date_int);
$week_number = date('W', $date_date);
echo "Weeknumber: {$week_number}.";
Also, your variable names will be confusing to you after a week of not looking at that code, you should consider reading http://net.tutsplus.com/tutorials/php/why-youre-a-bad-php-programmer/
The rule is that the first week of a year is the week that contains the first Thursday of the year.
I personally use Zend_Date for this kind of calculation and to get the week for today is this simple. They have a lot of other useful functions if you work with dates.
$now = Zend_Date::now();
$week = $now->get(Zend_Date::WEEK);
// 10
To get Correct Week Count for Date 2018-12-31 Please use below Code
$day_count = date('N',strtotime('2018-12-31'));
$week_count = date('W',strtotime('2018-12-31'));
if($week_count=='01' && date('m',strtotime('2018-12-31'))==12){
$yr_count = date('y',strtotime('2018-12-31')) + 1;
}else{
$yr_count = date('y',strtotime('2018-12-31'));
}
function last_monday($date)
{
if (!is_numeric($date))
$date = strtotime($date);
if (date('w', $date) == 1)
return $date;
else
return date('Y-m-d',strtotime('last monday',$date));
}
$date = '2021-01-04'; //Enter custom date
$year = date('Y',strtotime($date));
$date1 = new DateTime($date);
$ldate = last_monday($year."-01-01");
$date2 = new DateTime($ldate);
$diff = $date2->diff($date1)->format("%a");
$diff = $diff/7;
$week = intval($diff) + 1;
echo $week;
//Returns 2.
try this solution
date( 'W', strtotime( "2017-01-01 + 1 day" ) );

PHP Check if current time is before specified time

I need to check in PHP if the current time is before 2pm that day.
I've done this with strtotime on dates before, however this time it's with a time only, so obviously at 0.00 each day the time will reset, and the boolean will reset from false to true.
if (current_time < 2pm) {
// do this
}
if (date('H') < 14) {
$pre2pm = true;
}
For more information about the date function please see the PHP manual. I have used the following time formatter:
H = 24-hour format of an hour (00 to 23)
Try:
if(date("Hi") < "1400") {
}
See: http://php.net/manual/en/function.date.php
H 24-hour format of an hour with leading zeros 00 through 23
i Minutes with leading zeros 00 to 59
You could just pass in the time
if (time() < strtotime('2 pm')) {
//not yet 2 pm
}
Or pass in the date explicitly as well
if (time() < strtotime('2 pm ' . date('d-m-Y'))) {
//not yet 2 pm
}
Use 24 hour time to get round the problem like so:
$time = 1400;
$current_time = (int) date('Hi');
if($current_time < $time) {
// do stuff
}
So 2PM equates to 14:00 in 24 hour time. If we remove the colon from the time then we can evaluate it as an integer in our comparison.
For more information about the date function please see the PHP manual. I have used the following time formatters:
H = 24-hour format of an hour (00 to 23)
i = Minutes with leading zeros (00 to 59)
You haven't told us which version of PHP you're running, although, assuming it's PHP 5.2.2+ than you should be able do it like:
$now = new DateTime();
$twoPm = new DateTime();
$twoPm->setTime(14,0); // 2:00 PM
then just ask:
if ( $now < $twoPm ){ // such comparison exists in PHP >= 5.2.2
// do this
}
otherwise, if you're using one of older version (say, 5.0) this should do the trick (and is much simplier):
$now = time();
$twoPm = mktime(14); // first argument is HOUR
if ( $now < $twoPm ){
// do this
}
If you want to check whether the time is before 2.30 pm ,you can try the following code segment .
if (date('H') < 14.30) {
$pre2pm = true;
}else{
$pre2pm = false;
}
Try with
if( time() < mktime(14, 0, 0, date("n"), date("j"), date("Y")) ) {
// do this
}
This function will check if it's between hours in EST by accepting 2 params, arrays with the hour and am/pm...
/**
* Check if between hours array(12,'pm'), array(2,'pm')
*/
function is_between_hours($h1 = array(), $h2 = array())
{
date_default_timezone_set('US/Eastern');
$est_hour = date('H');
$h1 = ($h1[1] == 'am') ? $h1[0] : $h1[0]+12;
$h1 = ($h1 === 24) ? 12 : $h1;
$h2 = ($h2[1] == 'am') ? $h2[0] : $h2[0]+12;
$h2 = ($h2 === 24) ? 12 : $h2;
if ( $est_hour >= $h1 && $est_hour <= ($h2-1) )
return true;
return false;
}
Use time(), date() and strtotime() functions:
if(time() > strtotime(date('Y-m-d').' 14:00') {
//...
}

Categories