The code below would set open and closed on my website. But it is not in the right time zone. How do i set it to the Dutch time zone?
So can some one help me out.
And also i have a question about setting it in days.
So you can set on the website the shippingtime depending on day. So on monday the shipping time is: send today. Or on sunday it is send on monday.
<?php
/**
* Based on the following business hours:
* (Note : I setup the hours for each day if they carry-over)
* everyday is open from 09:00 AM - 12:00 AM
* Sun/Sat open extra from 12:00 AM - 01:00 AM
*/
$storeSchedule = [
'Sun' => ['00:00 AM' => '00:00 AM'],
'Mon' => ['9:00 AM' => '00:00 PM'],
'Tue' => ['9:00 AM' => '05:00 PM'],
'Wed' => ['9:00 AM' => '05:00 PM'],
'Thu' => ['9:00 AM' => '05:00 PM'],
'Fri' => ['9:00 AM' => '05:30 PM'],
'Sat' => ['9:00 AM' => '04:30 PM']
];
// current OR user supplied UNIX timestamp
$timestamp = time();
// default status
$status = 'momenteel gesloten, stuur ons een mail';
// get current time object
$currentTime = (new DateTime())->setTimestamp($timestamp);
// loop through time ranges for current day
foreach ($storeSchedule[date('D', $timestamp)] as $startTime => $endTime) {
// create time objects from start/end times
$startTime = DateTime::createFromFormat('h:i A', $startTime);
$endTime = DateTime::createFromFormat('h:i A', $endTime);
// check if current time is within a range
if (($startTime < $currentTime) && ($currentTime < $endTime)) {
$status = '<b>bereikbaar op: Tel: 023-5313188</b> of <b>mail</b>';
break;
}
}
echo "Onze klantenservice is $status";
?>
use this for instantiating the current DateTime Object in Dutch Timezone:
$current = new DateTime(
'now',
new DateTimeZone('Europe/Amsterdam')
);
the problem was at the DateTime comparison. Creating a DateTime object from only the time sets the date to 1970-01-01...
/**
* Based on the following business hours:
* (Note : I setup the hours for each day if they carry-over)
* everyday is open from 09:00 AM - 12:00 AM
* Sun/Sat open extra from 12:00 AM - 01:00 AM
*/
$storeSchedule = [
'Sun' => ['00:00 AM' => '00:00 AM'],
'Mon' => ['9:00 AM' => '00:00 PM'],
'Tue' => ['9:00 AM' => '05:00 PM'],
'Wed' => ['9:00 AM' => '05:00 PM'],
'Thu' => ['9:00 AM' => '05:00 PM'],
'Fri' => ['9:00 AM' => '05:30 PM'],
'Sat' => ['9:00 AM' => '04:30 PM']
];
// current OR user supplied UNIX timestamp
$timestamp = '2015-03-12 12:00:00'; // shows it is open
//$timestamp = '2015-03-12 8:59:59'; // shows closed
//$timestamp = '2015-03-12 9:00:00'; // shows open
//$timestamp = 'now'; // shows current status
// default status
$status = 'momenteel gesloten, stuur ons een mail';
// default timezone
$timeZone = new DateTimeZone('Europe/Amsterdam');
// get current time object
$currentTime = new DateTime($timestamp, $timeZone);
// loop through time ranges for current day
foreach ($storeSchedule[date('D', $timestamp)] as $startTime => $endTime) {
// create time objects from start/end times
$startTime = DateTime::createFromFormat(
'Y-m-d h:i A',
$currentTime->format('Y-m-d') . ' ' . $startTime,
$timeZone
);
$endTime = DateTime::createFromFormat(
'Y-m-d h:i A',
$currentTime->format('Y-m-d') . ' ' . $endTime,
$timeZone
);
// check if current time is within a range
if (($startTime <= $currentTime) && ($currentTime <= $endTime)) {
$status = '<b>bereikbaar op: Tel: 023-5313188</b> of <b>mail</b>';
break;
}
}
echo "Onze klantenservice is $status";
Related
I am wondering if there is a simpler / better way to handle this. Basically someone has a schedule from 9:00 AM to 5:00 PM and request an absence from 11:00 - 1:00 PM.
It would be split into:
[
['start'=> '9:00 AM', 'end' => '11:00 AM'],
['start'=> '11:00 AM', 'end' => '1:00 PM'],
['start'=> '1:00 PM', 'end' => '5:00 PM'],
]
My approach seems wrong, as I would be manually creating scenarios for each case.
Here is an example of what I am trying.
function splitTimesByStartAndEndTime( $days, $requested_start_time, $requested_end_time) {
$retval = [];
foreach ( $days as $day ) {
if ( strtotime( $day[ 'start_time' ] ) <= $requested_start_time && strtotime( $day[ 'end_time' ] ) >= $requested_end_time ) {
$retval[$day['start_date']][] = [
'start_time' => $day['start_time'],
'end_time' => $requested_start_time,
];
$retval[$day['start_date']][] = [
'start_time' => $requested_end_time,
'end_time' => $requested_start_time,
];
$retval[$day['start_date']][] = [
'start_time' => $requested_end_time,
'end_time' => $day['end_time'],
];
}
return $retval;
}
And need to handle scenarios where the request is not just in the middle. Such at goes from start to middle of shift, or middle of shift to end. Then also edge case where request is before start of shift etc, so bounds checking.
I have a php script which I finally got with the help of friends and stackoverflow, where I am trying to display the hours of operation of a store such as it displays the current status whether it is open or not and the next staus as well. That means if the store is not open currently then when it will open next also needs to display. The script code is given below: it works some time when store is not opened currently and will open same day again (in case of Sunday, Thursday and Saturday when it opens twice a day), but it never works when it is currently closed and will now open on next day some time. It gives some error on last line of scrip and the error is :
Fatal error: Call to a member function format() on a non-object in E:\xampp\htdocs\karnalguide\operation-hrs-test.php on line 73
I am making some mistake which I am not able to figure out - Please help me guys to fix this issue - check the error in script and help me with the fix. I am not much expert in php object oriented programming. So please explain me with full fix of this issue. Thanks in advance!
<?php
/* Script for displaying operation times of a store and will be used in https://www.karnalguide.com */
/* array of store opening timings */
$storeSchedule = [
'Sun' => [['12:00' => '01:00', '10:00' => '12:00']],
'Mon' => [['09:00' => '12:00']],
'Tue' => [['09:00' => '12:00']],
'Wed' => [['09:00' => '12:00']],
'Thu' => [['09:00' => '12:00'], ['22:50' => '23:00']],
'Fri' => [['09:00' => '12:00']],
'Sat' => [['12:00' => '01:00', '09:00' => '12:00']]
];
// current or user supplied UNIX timestamp
$timestamp = time();
// default status
$open = false;
// Open later at
$openAt = false;
// get current time object
$currentTime = (new DateTime())->setTimestamp($timestamp);
// Current day
$currentDay = date('D', $timestamp);
if(isset($storeSchedule[$currentDay])){
// loop through time ranges for current day
foreach ($storeSchedule[$currentDay] as $key => $dateGroup) {
$startTime = current(array_keys($dateGroup));
$endTime = current(array_values($dateGroup));
// create time objects from start/end times
$startTime = DateTime::createFromFormat('H:i', $startTime);
$endTime = DateTime::createFromFormat('H:i', $endTime);
// check if current time is within a range
if (($startTime < $currentTime) && ($currentTime < $endTime)) {
$open = true;
break;
}elseif($currentTime < $startTime){
// Opening Later
$openAt = $startTime;
}
}
}else{
// Not open because day is not in array
}
if($open){
echo "We are open";
}else{
if($openAt){
echo "We open later at " . $openAt->format('H:i');
}else{
// Get next open
$arrayDays = array_keys($storeSchedule); // Get an array of the days
$arrayTimes = array_values($storeSchedule); // Get an array of times
$dayIndex = array_search($currentDay, $arrayDays); // Find out what day we are in in the array. To see if there are more this week
$nextDay = ($dayIndex + 1) >= count($arrayDays) ? $arrayTimes[0] : $arrayTimes[$dayIndex + 1]; // If there are no more this week, take the first day, else take the next day.
$nextOpenTime = current(array_keys($nextDay)); // Take the first set of times from this day as the start time
$nextOpenDay = $arrayDays[$dayIndex + 1]; // Get the day key name
echo "We are not open";
echo "We open next on " . $nextOpenDay . " at " . $nextOpenTime->format('H:i');
}
}
?>
I believe this code will do the same in a shorter way.
I loop today and see if it's open or will open today.
If it's not open I dump the next day but that can easily be replaced with a echo.
$storeSchedule = [
'Sun' => [['12:00' => '01:00', '10:00' => '12:00']],
'Mon' => [['09:00' => '12:00']],
'Tue' => [['09:00' => '12:00']],
'Wed' => [['09:00' => '12:00']],
'Thu' => [['09:00' => '12:00'], ['22:50' => '23:00']],
'Fri' => [['09:00' => '12:00']],
'Sat' => [['12:00' => '01:00', '09:00' => '12:00']]
];
$today = date("D");
$closed = Null;
foreach($storeSchedule[$today] as $opentimes){
foreach($opentimes as $open => $close){
$now = time();
if($now > strtotime(date("Y-m-d" . $open)) && $now < strtotime(date("Y-m-d" . $close))){
echo "store open now";
$closed = false;
}else if($now < strtotime(date("Y-m-d" . $open))){
echo "store will open at " . date("Y-m-d" . $open);
$closed = false;
}else{
// Store is closed
if($closed !== false) $closed = true;
}
}
}
if($closed){
// Dump the next open time
var_dump($storeSchedule[date("D",strtotime($today)+86400)]);
}
https://3v4l.org/DAvmo
I'd like to include some text on my website that states whether or not a shop is open based on its opening times. If the shop is open, it says when it's open until. If it's not open, it says when it's next open.
I already have the opening times stored in the following variable:
$opening_times = [
'Monday' => ['09:00' => '17:00'],
'Tuesday' => ['09:00' => '17:00'],
'Wednesday' => ['09:00' => '12:00'],
'Thursday' => ['09:00' => '17:00'],
'Friday' => ['09:00' => '17:00'],
'Saturday' => ['09:30' => '17:00']
];
The shop is closed on Sunday.
Please could someone guide me as to how I can do this? I've already looked at this example but I'm unsure how to handle showing the time the shop is open next and what to do when it's a Sunday.
I'm hoping to finish with something that displays the next time the shop's open, whether that's on the same day or not. For example, at 5.30pm on Saturday, I'd like the message to say that the shop's next open at 9am on Monday.
I had previously attempted this by storing the next open day and time with each day in the $opening_times variable but I was wondering if there was a more elegant solution.
Using the answer here: Determine If Business Is Open/Closed Based On Business Hours as a guide.
This takes into account opening later and not opening today at all.
UPDATE: Tells you when it is next open. (Untested because work servers use PHP 5.3 :()
<?php
$storeSchedule = [
'Sun' => [['12:00' => '01:00', '09:00' => '12:00']],
'Mon' => [['09:00' => '12:00']],
'Tue' => [['09:00' => '12:00']],
'Wed' => [['09:00' => '12:00']],
'Thu' => [['09:00' => '12:00'], ['22:50' => '23:00']],
'Fri' => [['09:00' => '12:00']],
'Sat' => [['12:00' => '01:00', '09:00' => '12:00']]
];
// current or user supplied UNIX timestamp
$timestamp = time();
// default status
$open = false;
// Open later at
$openAt = false;
// get current time object
$currentTime = (new DateTime())->setTimestamp($timestamp);
// Current day
$currentDay = date('D', $timestamp);
if(isset($storeSchedule[$currentDay])){
// loop through time ranges for current day
foreach ($storeSchedule[$currentDay] as $key => $dateGroup) {
$startTime = current(array_keys($dateGroup));
$endTime = current(array_values($dateGroup));
// create time objects from start/end times
$startTime = DateTime::createFromFormat('H:i', $startTime);
$endTime = DateTime::createFromFormat('H:i', $endTime);
// check if current time is within a range
if (($startTime < $currentTime) && ($currentTime < $endTime)) {
$open = true;
break;
}elseif($currentTime < $startTime){
// Opening Later
$openAt = $startTime;
}
}
}else{
// Not open because day is not in array
}
if($open){
echo "We are open";
}else{
if($openAt){
echo "We open later at " . $openAt->format('H:i');
}else{
// Get next open
$arrayDays = array_keys($storeSchedule); // Get an array of the days
$arrayTimes = array_values($storeSchedule); // Get an array of times
$dayIndex = array_search($currentDay, $arrayDays); // Find out what day we are in in the array. To see if there are more this week
$nextDay = ($dayIndex + 1) >= count($arrayDays) ? $arrayTimes[0] : $arrayTimes[$dayIndex + 1]; // If there are no more this week, take the first day, else take the next day.
$nextOpenTime = current(array_keys($nextDay)); // Take the first set of times from this day as the start time
$nextOpenDay = $arrayDays[$dayIndex + 1]; // Get the day key name
echo "We are not open";
echo "We open next on " . $nextOpenDay . " at " . $nextOpenTime->format('H:i');
}
}
?>
Date and time are setted to Madrid's standard UTC, and stored for translation and formatting in this way:
date_default_timezone_set('Europe/Madrid');
$dia=""; $mes=""; $dia2=""; $ano=""; $horaActual=""; $minutoActual="";
$dia=date("l");
if ($dia=="Monday") {$dia="Lunes";} if ($dia=="Tuesday") {$dia="Martes";} if ($dia=="Wednesday") {$dia="Miércoles";} if ($dia=="Thursday") {$dia="Jueves";} if ($dia=="Friday") {$dia="Viernes";} if ($dia=="Saturday") {$dia="Sabado";} if ($dia=="Sunday") {$dia="Domingo";}
$mes=date("F");
if ($mes=="January") {$mes="Enero";} if ($mes=="February") {$mes="Febrero";} if ($mes=="March") {$mes="Marzo";} if ($mes=="April") {$mes="Abril";} if ($mes=="May") {$mes="Mayo";} if ($mes=="June") {$mes="Junio";} if ($mes=="July") {$mes="Julio";} if ($mes=="August") {$mes="Agosto";} if ($mes=="September") {$mes="Setiembre";} if ($mes=="October") {$mes="Octubre";} if ($mes=="November") {$mes="Noviembre";} if ($mes=="December") {$mes="Diciembre";}
$dia2=date("d");
$ano=date("Y");
$horaActual=date("H");
$minutoActual=date("m");
This gives the same time and date all the time (I created this an hour ago), not refreshing while web browser does. In this right moment, this code:
<?php echo "$dia $dia2 de $mes, $horaActual:$minutoActual"; echo "--" date("F j, Y, g:i a");?>
Shows:
Lunes 26 de Mayo, 16:05 -- May 26, 2014, 5:03 pm
So date() is getting the correct and updated info, but variables are not updating this info, showing stucked data from the first time they stored this values.
every time user gets inside this url, date and time must be updated with actual values
I dont know how your time got stuck, but alternatively you could do this (time updated). Consider this example:
date_default_timezone_set('Europe/Madrid');
$dia = $mes = $dia2 = $ano = $horaActual = $minutoActual = "";
$days = array('Monday' => 'Lunes', 'Tuesday' => 'Martes', 'Wednesday' => 'Miércoles', 'Thursday' => 'Jueves', 'Friday' => 'Viernes', 'Saturday' => 'Sabado', 'Sunday' => 'Domingo');
$months = array('January' => 'Enero', 'February' => 'Febrero', 'March' => 'Marzo', 'April' => 'Abril', 'May' => 'Mayo', 'June' => 'Junio', 'July' => 'Julio', 'August' => 'Agosto', 'September' => 'Setiembre', 'October' => 'Octube', 'November' => 'Noviembre', 'December' => 'Diciembre');
$dia = date("l");
$mes = date("F");
$dia2 = date("d");
$ano = date("Y");
// $horaActual = date("H");
// $minutoActual = date("m");
$time = date('H:i');
echo "$days[$dia] $dia2 de $months[$mes], $time"; echo "--". date("F j, Y, g:i a");
// outputs: Lunes 26 de Mayo, 17:21--May 26, 2014, 5:21 pm
Fiddle
How can I display the date in Turkish language?
I'm trying following code but it does not print anything at all.
setlocale(LC_ALL, 'tr_TR.UTF-8');
echo strftime("%e %B %Y %A", time());
Though i don't understand turkish but it is printing output
14 Şubat 2013 Perşembe
So your code is fine.
Hope you are not missing out php tags. :/
<?php
setlocale(LC_ALL, 'tr_TR.UTF-8');
echo strftime("%e %B %Y %A", time());
?>
Prints out: 30 Ekim 2015 Cuma
Code works fine.
Php date for "F" which gives month of English name to translate to Turkish given example
date("d F")
you can use this function
function convertMonthToTurkishCharacter($date){
$aylar = array(
'January' => 'Ocak',
'February' => 'Şubat',
'March' => 'Mart',
'April' => 'Nisan',
'May' => 'Mayıs',
'June' => 'Haziran',
'July' => 'Temmuz',
'August' => 'Ağustos',
'September' => 'Eylül',
'October' => 'Ekim',
'November' => 'Kasım',
'December' => 'Aralık',
'Monday' => 'Pazartesi',
'Tuesday' => 'Salı',
'Wednesday' => 'Çarşamba',
'Thursday' => 'Perşembe',
'Friday' => 'Cuma',
'Saturday' => 'Cumartesi',
'Sunday' => 'Pazar',
'Jan' => 'Oca',
'Feb' => 'Şub',
'Mar' => 'Mar',
'Apr' => 'Nis',
'May' => 'May',
'Jun' => 'Haz',
'Jul' => 'Tem',
'Aug' => 'Ağu',
'Sep' => 'Eyl',
'Oct' => 'Eki',
'Nov' => 'Kas',
'Dec' => 'Ara'
);
return strtr($date, $aylar);
}
add it before the code.
setlocale (LC_ALL, 'tr_TR.UTF-8', 'tr_TR', 'tr', 'turkish');
public function getTurkishDate(){
$locale = 'tr_TR'; // a canonicalized locale
$format = 'dd-MMMM-YYYY'; // ISO format codes, not the typical date ones
$dt = new DateTime(); // a DateTime object
$df = new IntlDateFormatter(
$locale, // string locale
IntlDateFormatter::NONE, // int date type
IntlDateFormatter::NONE, // int time type
'UTC', // string timezone
IntlDateFormatter::GREGORIAN, // int cal type
$format // string pattern
);
return $df->format($dt); //string 07-Ağustos-2018
}
OR
public function getTurkishDate(){
$locale = 'tr_TR'; // a canonicalized locale
$format = 'dd-MMMM-YYYY-EEEE'; // ISO format codes, not the typical date ones
$dt = new DateTime(); // a DateTime object
$df = new IntlDateFormatter(
$locale, // string locale
IntlDateFormatter::NONE, // int date type
IntlDateFormatter::NONE, // int time type
'UTC', // string timezone
IntlDateFormatter::GREGORIAN, // int cal type
$format // string pattern);
return $df->format($dt); //string 07-Ağustos-2018-Salı
}
OR
public function getTurkishDate(){
$locale = 'tr_TR'; // a canonicalized locale
$format = 'dd-MMMM-YYYY-ee'; // ISO format codes, not the typical date ones
$dt = new DateTime(); // a DateTime object
$df = new IntlDateFormatter(
$locale, // string locale
IntlDateFormatter::NONE, // int date type
IntlDateFormatter::NONE, // int time type
'UTC', // string timezone
IntlDateFormatter::GREGORIAN, // int cal type
$format // string pattern);
return $df->format($dt); //string 07-Ağustos-2018-02
}
You can take current date and time with:
<?php
date_default_timezone_set("Europe/Istanbul");
//echo "The time is " . date("H:i") . "<br>";
//echo "Today is " . date("Y-m-d") . "<br>";
//echo "Today is " . date("l");
$now_time = date("H:i");
$now_date = date("Y-m-d");
?>