Determining whether the current time is before 5pm that day - php

I want to have code in a loop that only runs if it is before 5pm on any day.
What code would do this?
IF (current_time < 5:00pm) {
// Do stuff
}

This solution uses the nice PHP5 DateTime class, and avoids the clunky old strtotime() function:
$hoursNow = new DateTime()->format('H');
if($hoursNow < 17) {
....
}

Simple one that gets the job done:
if (date('H') < 17)
{
// DO SOMETHING
}
Just remember that this date comes from the server. So, if the client is in another timezone, that may not work as required. The good thing about this, is that the user can't change his computer date in order to trick the site into something.
And if you want to do that with javascript (where the value will come from the user's computer), just do the following:
var today = new Date();
if (today.getHours() < 17)
{
// DO SOMETHING
}

if(time() < strtotime('today 05:00 pm')) {
// Do stuff
}

In short...
$t = time(); # or any other timestamp
if (date('H', $t) < 17) {
// Do stuff
}
Works for any day
Note: Make sure your timezone is correct (check php config or just use date_default_timezone_set function before calling date function)

Related

How can I use PHP to display a variable every other day at a certain time?

Context
I would like to use PHP to change out content in HTML every other day. I can currently achieve this, however, I would need the content to change at 7:00 AM PST. Right now, I'm essentially subtracting the current time from a set point in time using mktime and rounding down. A simple if statement takes care of the variable.
The Code
<?php
$first_date = mktime(7,0,0,1,1,2014);
$second_date = time();
$offset = $second_date-$first_date;
$this_day = floor($offset/60/60/24);
if ($this_day % 2 == 0) {
//do something
} else {
//do something else
}
?>
Like I stated before, the code is working however it is not changing at 7:00 AM PST. I tried adjusting the start time in $first_date however it didn't seem to help. I think I am missing something regarding actual timezones and how the time is being calculated. Any insight would be greatly appreciated.
I just take the start date and gave it a timezone, then checked the day number of the year. If it's even and 7am or later, do something. Otherwise, do something else. The only issue you will have is New Years. This may show the same content two days in a row.
<?php
$date = new DateTime(null, new DateTimeZone('America/Los_Angeles'));
if ($date->format('z') % 2 == 0 && $date->format('h') >= 7) {
//do something
} else {
//do something else
}
?>

Working with UTC and php

I'm using Live Connect to create calendar events. According to their docs, the start_time given for an event should indicate how many hours off the time is from UTC (i.e. +0700 or -0300). As a first stab , I've got some code that works, pieced together from the php manual. However, it "feels" pretty verbose. So, from a stylistic point of view, might there be a way to clean up what I've got into something more succinct? Note that the $time_zone is something that I know based on a given user.
$dateTimeZone = new DateTimeZone($time_zone);
$dateTime= new DateTime("now", $dateTimeZone);
$gmt_offset = ($dateTime->getOffset())/3600;
$negative = ($gmt_offset<0);
$gmt_offset = abs($gmt_offset);
if ($gmt_offset < 10) {
$gmt_offset = '0'.$gmt_offset.'00';
} else {
$gmt_offset = $gmt_offset.'00';
}
if ($negative) {
$gmt_offset = '-'.$gmt_offset;
} else {
$gmt_offset = '+'.$gmt_offset;
}
Thank you for your input.
-Eric
$gmt_offset = $dateTime->format('O');
From the PHP manual page for date():
format character: O
Description: Difference to Greenwich time (GMT) in hours
Example returned values: Example: +0200

How to turn php time function into javascript function?

I'm not that good at javascript (yet), so I need some help, with an alternative version of this php script (In javascript)
function until($format = ""){
$now = strtotime("now");
$nextTuesday = strtotime("-1 hour next tuesday");
$until = $nextTuesday - $now;
if(empty($format)){
return $until;
}else{
return date("$format",$until);
}
}
Just need it to count down, until next tuesday, in a really short way (Not in 20+ lines, like all the other script I've seen)
It should still return a timestamp, if it's possible (Need it for an offline app)
So if anyone could help me, I would be really happy (Not that I'm not happy right now, but I would be even happier) :D
You may want to take a look at the phpjs site. They have code showing how a substantial number of PHP functions can be done in JS.
Specifically: strtotime and date
JS doesn't have anything remotely close to strtotime. You'd have to determine "next tuesday" yourself. Once you've got that, you can extract a timestamp value using .getTime(), which will be the number of milliseconds since Jan 1/1970. This value can also be fed back into a new date object as a parameter, so you can do date math using simple numbers externally, then create a new date object again using the result.
e.g.
var now = new Date();
var ts = now.getTime();
var next_week = ts + (86400 * 7 * 1000);
next_week_object = new Date(next_week);
Once you've got the "next tuesday" code figured out, the rest is trivial
To get milliseconds till the next tuesday (nearest in the future):
function f_until(){
var now = new Date(Date.now());
var nextT = new Date(Date.now());
var cD = nextT.getDay();
if(cD < 2)nextT.setDate(nextT.getDate() + (2-cD));
else nextT.setDate(nextT.getDate() + (9-cD));
nextT.setHours(nextT.getHours() - 1);
//alert('next tuesday: '+nextT.toString());
return nextT.getTime() - now.getTime();
}

Comparing times in the 12-hour format

Can someone help and give a solution to compare two times which are in the following format when received: 12:20 AM.
I have two time fields in my script which are meant to be stored in the database after validation, but after validation that the format is in correct order i want to do comparison between those two fields.
The comparison i want to do is to check if the second field not overlapping the first one.
For Example:
The first variable is named $timeStart, and holding the time value of = "05:30 PM".
The second is named $timeEnd, holding a value of = "4:00 PM".
Now, i want to do a comparison to see if the $timeEnd is before $timeStart (also putting into consideration AM/PM when comparing) if it is i dont want to echo an error, otherwise continue with the script.
I'm doing it for a Event Script, so timing has to be right :) $timeEnd has to be after $timeStart.
I sure do hope i explained it clear enough :(
Thanks for taking the time to read my complicated gibberish :)
if(function greaterDate($start_date,$end_date)
{
$start = strtotime($start_date);
$end = strtotime($end_date);
if ($start-$end > 0)
return 1;
else
return 0;
}
$time = greaterDate($start_date,$end_date))
This should help
function timediff($start, $end)
{
if($end >= $start)
{
return (round(($end-$start)/3600, 0))." Hrs ".((($end-$start)%3600)/60)." Min";
}
return false;
}
echo timediff(strtotime('yesterday 5 pm'), strtotime('today 4:30 am'));
if (strtotime($timeEnd) < strtotime($timeStart)) {
// fail
}
Only makes sense though if you're not going to allow day crossovers, obviously.

if php date is 24 hours after stored mysql date

I'm trying to do a function that will check the php date against what is in the database. If the current date (YYYY-MM-DD HH:MM:SS) is 24 hours after the stored database date, then it will continue, otherwise it won't do anything.. If that makes sense?
Here's how I'm trying to get it to work, but not 100% sure how:
$now = date("Y-m-d H:i:s");
foreach($results as $item) {
if ( /*item date is 24 hours or more earlier than $now*/ ) { /* this is what I'm not sure how to do */
//do something
} else {
//do nothing
};
};
If someone could help me get this working, I'd greatly appreciate it. I just don't know how to compare $item->date with $now and see if $item->date is 24 hours or more behind $now..
You can use strtotime instead of date math. Personally I think it reads much better.
if (strtotime($item->date) <= strtotime('-1 day')) {
// date is more than or equal to 24 hours old
}
foreach($results as $item) {
if (time() >= strtotime($item['date']) + 86400) {
// current time is 86400 (seconds in one day) or more greater than stored time
} else {
//do nothing
};
};
Note that if the stored date isn't in the same timezone as the server you'll need to mess with timezones - see here for starters.
Note that I store dates and times in MySQL as Unix timestamps to avoid some of these problems. I know it's not good database practice, but then PHP stores times as Unix timestamps anyway.

Categories