I'm trying to build a PHP script so that when a store is opened it will show the words "is now open" and when it's closed it'll show "is now closed" based on timezone and hours. I can't seem to get this to work, I found this script but it doesn't work properly.. It should work on the dutch timezone.. +1 Amsterdam.. Can anyone aid me?
I'd appreciate the help!
SCRIPT:
<?php
$schedule[0] = "700-1730";
$schedule[1] = "700-1730";
$schedule[2] = "700-1730";
$schedule[3] = "700-1730";
$schedule[4] = "10:00-17:30";
$schedule[5] = "700-1300";
$schedule[6] = "0";
$today = $schedule[date('w')];
list($open, $close) = explode('-', $schedule);
$now = (int) date('Gi');
$state = 'is geopend.';
if ($today[0] == 0 || $now < (int) $today[0] || $now > (int) $today[1]) {
$state = 'is gesloten.';
}
?>
<?php echo $state;?>
The colon is causing a bit of a problem so you can remove it with a quick str_replace();
Instead of $schedule you want to explode(); $today once you have made it.
<?php
$schedule[0] = "700-1730";
$schedule[1] = "700-1730";
$schedule[2] = "700-1730";
$schedule[3] = "700-1730";
$schedule[4] = "10:00-17:30";
$schedule[5] = "700-1300";
$schedule[6] = "0";
$today = $schedule[date('w')];
$now = (int) date('Gi');
list($open, $close) = explode('-', $today);
// get rid of colon if you have used one
$open = (int) str_replace(':', '', $open);
$close = (int) str_replace(':', '', $close);
$state = ' is geopend.';
if ($today[0] == 0 || $now < $open || $now > $close){
$state = ' is gesloten.';
}
?>
Depending on the setup of your server, if you don't want to dynamically reset timezone in your ini settings, you could add 100 per hour you want to adjust by, so if you have hours time difference in your database you could make a variable $time_difference * 100 and add it to or subtract it from $now = (int) date('Gi'); so, for example $now = (int) date('Gi') + 700; would give you European Central Time when your server time is based on New York time or $now = (int) date('Gi') + $time_difference * 100; could be used to adjust it dynamically. (You would need to account for daylight saving time though!)
Time reference for the right way to set timezones: PHP date(); with timezone?
In case you wanted to play with using the user's own timezone to display your opening times and adjust accordingly for a bit of fun - may not be advisable for a live project as there seem to be few totally reliable ways. Determine a User's Timezone
Please note that the numbers for days work from 0 for Sunday to 6 for Saturday http://php.net/manual/en/function.date.php
Related
Just wanted to double check and make sure my line of thinking is correct. This hook runs every 48 hours, so I need to check if an event is happening today or tomorrow.
$now = date('Y/m/d');
$today = explode("/", $now);
The event start date has the same format, but the value varies, and is stored the same way.
if ( $today[1] == $eventDate[1] &&
(intval($today[2]) == (intval($eventDate[2]-1)) || (intval($today[2]) == intval($eventDate[2])-2))) {
//run code
}
In my opinion, you should consider to work with DateTime objects :
$today = new \DateTime();
$eventDateTime = \DateTime::createFromFormat('Y/m/d', $eventDate);
if ($today->format('d-m-Y') === $eventDateTime->format('d-m-Y')) {
...
}
If you want to check that an event is happening today or tomorrow, you could do like this :
$start = new \DateTime();
$start->setTime(0,0,0);
$end = new \DateTime();
$end->add(new \DateInterval('P1D'));
$end->setTime(23,59,59);
$eventDateTime = \DateTime::createFromFormat('Y/m/d', $eventDate);
if ($eventDateTime >= $start && $eventDateTime <= $end) {
...
}
This way you check if a date is in a certain period. Here I added 1 day to the current date but you can adapt the code and set more than 1 day if you want.
I am having table time in mysql database with one attribute of type "TIME" which contains default value "09:00:00". what I am trying to do is to get this value and subtract it from current time.
include 'connection.php';
$time = mysql_query("SELECT start_time FROM time");
$s = mysql_fetch_assoc($time);
$start_time = strtotime($s['start_time']);
$time_now = date("H:i:s");
$delay = ($time_now - $start_time);
However it never worked the way I need. result always like 00:00:00
what i want to achieve is something like:
$start_time = 09:00:00
$time_now = 09:34:23
so $delay should be 00:34:23.
any help to achieve that?
Thanks in advance.
Just a small bug in your code.... look below
include 'connection.php';
$time = mysql_query("SELECT start_time FROM time");
$s = mysql_fetch_assoc($time);
$start_time = strtotime($s['start_time']);
$time_now = date("H:i:s");
$delay = ($time_now - $start_time); //BUG! String minus timestamp here...
Fix that second block with:
$start_time = strtotime($s['start_time']);
$delay = date( "H:i:s", time() - $start_time );
Agree with others though, this is really cleanly done on the database side as well.
Hope this helps
Am trying to get the time difference between two days. But for certain date/time, I get wrong answers
Here is my code:
/****************************************
$start_date = new DateTime('23:58:40'); *These two still give
$end_date = new DateTime('00:00:00'); *a wrong answer
*****************************************/
$start_date = new DateTime('23:58:40');
$end_date = new DateTime('00:11:36');
$dd = date_diff($end_date, $start_date);
//Giving a wrong answer: Hours = 23, Minutes = 47, Seconds = 4
echo "Hours = $dd->h, Minutes = $dd->i, Seconds = $dd->s";
The awnser is correct. You provide two times. Without a date there is no way to know the last date is actually the next day. Just because you named the variable "end_date" doesnt mean PHP knows what you mean.
Perhaps you should include the date aswell in your request like
$start_date = new DateTime('2012-12-07 23:58:40');
$end_date = new DateTime('2012-12-08 00:11:36');
If you realy want to work with just times:
function differenceInTimes($start, $end) {
if (strtotime($start)>strtotime($end)) {
//start date is later then end date
//end date is next day
$s = new DateTime('2000-01-01 '.$start);
$e = new DateTime('2000-01-02 '.$end);
} else {
//start date is earlier then end date
//same day
$s = new DateTime('2000-01-01 '.$start);
$e = new DateTime('2000-01-01 '.$end);
}
return date_diff($s, $e);
}
$start_date = '23:58:40';
$end_date = '00:11:36';
$dd = differenceInTimes($start_date, $end_date);
echo "Hours = $dd->h, Minutes = $dd->i, Seconds = $dd->s";
//Hours = 0, Minutes = 12, Seconds = 56
Swap the arguments to date_diff
$dd = date_diff($start_date, $end_date);
Edit
After actually testing this theory it proved to be totally useless, giving the same answer.
I have a task to create a script using php to display open and closed during the correct times. So far I have the time working correcty and this would be fine if the business was open during this time for 7 days a week. However the scenerio for the project is the business is open mon-fri 7:00am - 5:30 pm then open saturdays 7:00am to 1:00pm and closed sundays. I thought I could use a date function w since is displays 0-6 and call if
if($date >= 0 && $date < 6)
but that didn't work. Here is the code I have so far
<?php
date_default_timezone_set('America/Chicago');
$open = "700";
$close = "1730";
$time = date('Gi');
$day = date('w');
if ($time >= $open && $time <= $close) {
echo "We are Open";
} else {
echo "We are closed";
}
?>
If you're not using a database you can hardcode each day of the week in some easily parsable format:
$schedule[0] = "700-1730";
$schedule[1] = "700-1730";
$schedule[2] = "700-1730";
$schedule[3] = "700-1730";
$schedule[4] = "700-1730";
$schedule[5] = "700-1300";
$schedule[6] = "0";
$today = $schedule[date('w')];
list($open, $close) = explode('-', $schedule);
$now = (int) date('Gi');
$state = 'Open';
if ($today[0] == 0 || $now < (int) $today[0] || $now > (int) $today[1]) {
$state = 'Closed';
}
Just wrote the code, didn't test it yet.
Good luck!
Create DateTime objects for the open and close times. Then compare the current time as a DateTime object with those times. You can then use comparison operators. You can also then check the day and have it go in an if, elseif, and else statement for whether the day is a weekday, Saturday, or Sunday.
What's the most precise function you have come across to work out an age from the users date of birth. I have the following code and was wondering how it could be improved as it doesn't support all date formats and not sure if it's the most accurate function either (DateTime compliance would be nice).
function getAge($birthday) {
return floor((strtotime(date('d-m-Y')) - strtotime($date))/(60*60*24*365.2421896));
}
$birthday = new DateTime($birthday);
$interval = $birthday->diff(new DateTime);
echo $interval->y;
Should work
Check this
<?php
$c= date('Y');
$y= date('Y',strtotime('1988-12-29'));
echo $c-$y;
?>
Use this code to have full age including years, months and days-
<?php
//full age calulator
$bday = new DateTime('02.08.1991');//dd.mm.yyyy
$today = new DateTime('00:00:00'); // Current date
$diff = $today->diff($bday);
printf('%d years, %d month, %d days', $diff->y, $diff->m, $diff->d);
?>
Try using DateTime for this:
$now = new DateTime();
$birthday = new DateTime('1973-04-18 09:48:00');
echo $now->diff($birthday)->format('%y years'); // 49 years
See it in action
This works:
<?
$date = date_create('1984-10-26');
$interval = $date->diff(new DateTime);
echo $interval->y;
?>
If you tell me in what format your $birthday variable comes I will give you exact solution
WTF?
strtotime(date('d-m-Y'))
So you generate a date string from the current timestamp, then convert the date string back into a timestamp?
BTW, one of the reasons it's not working is that strtotime() assumes numeric dates to be in the format m/d/y (i.e. the US format of date first). Another reason is that the parameter ($birthday) is not used in the formula.
Change the $date to $birthday.
For supper accuracy you need to account for the leap year factor:
function get_age($dob_day,$dob_month,$dob_year){
$year = gmdate('Y');
$month = gmdate('m');
$day = gmdate('d');
//seconds in a day = 86400
$days_in_between = (mktime(0,0,0,$month,$day,$year) - mktime(0,0,0,$dob_month,$dob_day,$dob_year))/86400;
$age_float = $days_in_between / 365.242199; // Account for leap year
$age = (int)($age_float); // Remove decimal places without rounding up once number is + .5
return $age;
}
So use:
echo get_date(31,01,1985);
or whatever...
N.B. To see your EXACT age to the decimal
return $age_float
instead.
This function works fine.
function age($birthday){
list($day,$month,$year) = explode("/",$birthday);
$year_diff = date("Y") - $year;
$month_diff = date("m") - $month;
$day_diff = date("d") - $day;
if ($day_diff < 0 && $month_diff==0){$year_diff--;}
if ($day_diff < 0 && $month_diff < 0){$year_diff--;}
return $year_diff;
}
See BLOG Post
Here is my long/detailed version (you can make it shorter if you want):
$timestamp_birthdate = mktime(9, 0, 0, $birthdate_month, $birthdate_day, $birthdate_year);
$timestamp_now = time();
$difference_seconds = $timestamp_now-$timestamp_birthdate;
$difference_minutes = $difference_seconds/60;
$difference_hours = $difference_minutes/60;
$difference_days = $difference_hours/24;
$difference_years = $difference_days/365;