PHP function to compare now time to time in database - php

I am trying to show different content based on the time recorded in my database.
From 1800 pm to 0800 am show content B.
The rest of the remaining time show content A.
My database field is storing time field so in database 1800 pm is stored as 18:00:00 same goes to 0800 am, it is stored as 08:00:00.
Below is my logic to get content B when time is between 18:00:00 to 08:00:00:
$now = strtotime(date('H:i:s'));
$time_from = strtotime($data['date_from']); //18:00:00
$time_to = strtotime($data['date_to']); // 08:00:00
if($now >= $time_from && $now <= $time_to){
echo 'content A';
}
The above code will only work if my $time_to is within 23:59:59 as $now will always be bigger than 08:00:00. Lets say the time now is 23:30:00, my codes will never echo "content A" out because 23:30:00 is bigger than 08:00:00.
How can i make the logic work to check by time only then to display the content?
#All, im editing the code again. Yes. i did put $now = strtotime(date('H:i:s'));. But it is not working as well. Firstly, the current now unix timestamp will always be bigger than 08:00:00 unix timestamp. let's say the time now is 23:30:00. The unix timestamp will always be bigger than 08:00:00.

if (date("H:i") >= '08:00' && date("H:i") <= '18:00') {
// Retrieve content 'A'
} else {
// Retrieve content 'B'
}

before making the comparision, you need to strtotime() your $now variable as well, try:
$now = date('H:i:s');
$time_from = strtotime($data['date_from']); //18:00:00
$time_to = strtotime($data['date_to']); // 08:00:00
$now_str = strtotime($now);
if($now_str >= $time_from && $now_str <= $time_to){
echo 'content A';
}

Related

Converting hours to timestamps

To convert a date to timestamp, I usually do this- strtotime ("2018-05-17 05:04:34) but now, I want to convert just hours (without date) e.g. 02:00:00 to timestamp. How do I do this?
Why I need this is to compare if a certain time is greater than the hour specified. This is what I am doing:
$reported = strtotime("2018-05-17 05:04:34");
$respons = strtotime("2018-05-17 17:04:34);
$response_time = $respons - $reported;
I want to be to check if $response_time is greater than 1 hour.
There is DateTime::diff, which probably does what you need
https://secure.php.net/manual/de/datetime.diff.php
In your case that should be
$datetime1 = new DateTime("2018-05-17 05:04:34");
$datetime2 = new DateTime("2018-05-17 17:04:34);
$interval = $datetime1->diff($datetime2);
echo $interval->format('H hours');
Strtotime has no problem parsing a time without a date.
No need to fake a date which will come back and bite you with daylight savings.
I also added a check to see if the start/end is "reversed".
$start= "2018-05-17 05:04:34";
$end = "2018-05-17 17:04:34";
//Note that it's intentionally reversed
$diff = strtotime(substr($start,11))-strtotime(substr($end,11));
//If the calculation was reversed add one day in seconds
if($diff <0) $diff += 86400;
If($diff >3600){
Echo "more than one hour";
}Else{
Echo "less than one hour";
}
https://3v4l.org/g1jZH
I believe only I have understood your question correctly.
I want to convert just hours (without date) e.g. 02:00:00 to timestamp.
There's no Date component here.
Okay, I assume they are of same date. If that's the case, just append an arbitrary date in front of the two to make the strtotime() function work:
$start = "05:04:34";
$end = "17:04:34";
$reported = strtotime("2018-05-17 " . $start);
$respons = strtotime("2018-05-17 " . $end);
$response_time = $respons - $reported;
if ($response_time > 3600)
echo "More than hour!";
else
echo "Less than hour!";
Note: This doesn't work if the start time is 17:00 and end time is say, 08:00 - which occurs in the next day. You have to make sure if the start time is greater than end time, then you have to add one more day to the end time.
I like the DateTime class, give this a try:
<?php
$reported = new DateTime('2018-05-17 05:04:34');
$reported->modify('+2 hours');
$now = new DateTime();
echo $now < $reported ? 'less than 2 hours' : 'more than 2 hours';
See it here https://3v4l.org/T7BEL
See the DateTime class docs here http://php.net/manual/en/class.datetime.php

PHP Event booking: get time in increments of 30 minutes is 1 hour out

I am trying to get a select box with a list of time increments from 00:00 to 24:00.
The time increments are an associative array of seconds to the display time like this:
1800 => 00:30
Then when someone selects a date, I plan to convert the date (e.g. 2016/9/16) into seconds and then add the time that they selected on to those seconds to get the date time of their event.
To do this I am generating the time increments like this.
public function getIntervals($start_time = '00:00', $end_time = '24:00', $increments = '30')
{
$today = strtotime(date('Y/m/d', time())); // seconds for 2016/9/16
$seconds_start = strtotime(date('Y/m/d', time()) . ' ' . $start_time) - $today;
$seconds_end = strtotime(date('Y/m/d', time()) . ' ' . $end_time) - $today;
while ($seconds_start < $seconds_end) {
$intervals[$seconds_start] = date("H:i", $seconds_start);
$seconds_start = strtotime('+' . $increments . ' minutes', $seconds_start);
}
return $intervals;
}
However my time intervals are one hour out.
If someone selected '2016/9/17 at 01:00 in the morning' the value in seconds of 01:00 in the morning is '0'.
If I add '0' to strtoseconds('2016/9/17') I am going to get '2016/9/17 at 00:00'.
So here I am,one hour out. I assume this is due to my server time being set to paris +1hr, but I have no idea how to fix it.
$timediff = 3600;
$paris_00_time = strtotime(date('Y-m-d '.'00:00'));
$paris_selected_time = $paris_00_time+1800;
$my_time = date('Y-m-d H:i:s',$paris_selected_time-$timediff);
print_r($my_time);
you can declare $timediff=3600; ( 1 hour )
$paris_00_time = strtotime(date('Y-m-d '.'00:00'));
after someone choose the date ( ex: [1800] => 00:30 ) and is submitted you can process the date
$paris_selected_time = $paris_00_time+1800;
$my_time = date('Y-m-d H:i:s',$paris_selected_time-$timediff);
output: 2016-09-15 23:30:00
I don't really remember where, but you can change your timezone. Search by PHP timezone and you will find from the official website a list with all the possible country/city. You just need to set a variable with the country/city you need and that's it. I don't know if you can do it in your PHP script or need to change your config files in the server.

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
}

PHP need set date based on time

I have an simple question about how to set date based on time range. This is my code so far:
date_default_timezone_set("Asia/Jakarta");
$time = date("G:i");
if ($time >= 8:00)
{
echo date("j-F-Y");
}
else
{
echo date("j-F-Y", time() - 60 * 60 * 24);
}
Example today is 29-Apr-2013.
Now I want before time 8:00 the date will still 28-Apr-2013. After that, date will continue to 29-Apr-2013.
The code is successfully complete the rule, if time before 8:00. But if I changed my computer time to be 11:00 or etc, it will set yesterday back.
$time = date("G:i");
if ($time >= 8:00)
This comparison is not good. Try numerically like
$time = intval(date("Gi"));
if ($time >= 800)

Time difference not working when year changes

I have a script that was working, well is working but not properly. The function is suppose to work out the time difference between two dates/times.
The First date is the Current Date and Time (Date + Hr:Min) and the second date is chosen by a user.
The purpose is to show error when the current date/time is within 24 hours from the user chosen date. i.e. if today is 23/20/2012 16:00 and the user chooses 24/10/2012 15:00 (this mean its within 24 hours) but if user chooses 26/10/2012 19:00 then its passed 24 hours.
Now this works fine but when the date changes its year (when user selected any date after 31st Dec 2012.. it assumes its still within 24 hours.. and im quite baffled how this happens.. can anyone shed some light what I've done wrong?
$dt = $_GET['dt']; $tm = $_GET['tm'];
// Current Date an time (Hrs & Mins)
$date1 = date("Y-m-d H:i");
// Chosen Date/Time
$date2 = date("Y-m-d", strtotime( "$dt" ) );
$diff = strtotime($date2." $tm") + - strtotime($date1);
if($diff/3600 < 24)
echo "0";
else
echo "1";
The following is the corresponding Ajax that makes th call
function getAjaxTime()
{
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
{
alert ("Your browser does not support AJAX!");
return;
}
dt = document.frm.arrival_date.value;
tm = document.frm.arrival_hour.value +':'+document.frm.arrival_min.value;
xmlHttp.open("GET","<?php echo $base_dir;?>/admin/get/timediff.php?dt="+encodeURI(dt)+"&tm="+encodeURI(tm),false);
xmlHttp.send(null);
return xmlHttp.responseText;
}
I would try something like this:
function isDateWithin24Hours($targetDate, $currentDate = 'now') {
$targetDate = new DateTime($targetDate);
$currentDate = new DateTime($currentDate);
$interval = $targetDate->diff($currentDate);
//%a = total number of days
if ($interval->format('%a') > 1) {
return (int) false;
}
return (int) true;
}
echo isDateWithin24Hours('2012-10-24 19:00:00');
echo isDateWithin24Hours('2012-10-24 19:00:00', '2012-10-23 18:00:00');
According to the php manual - http://us3.php.net/manual/en/datetime.formats.date.php - your date is not a valid format:
24/10/2013 // with / as deliminators
These would be valid
24-10-2013 // with - as deliminators
10/24/2013 // with / as deliminators and as m/d/Y
Update-
Also, the following format is invalid in strtotime & date-
Thursday, 10 May, 2012 00:30
But this would be valid -
Thursday, May 10, 2012 00:30
Update #2
In fact once your $_GET['dt'] is in a valid php format, you could simplify your code to-
$dt = $_GET['dt']; $tm = $_GET['tm'];
// Current Date an time (Hrs & Mins)
$date1 = date("Y-m-d H:i");
$diff = strtotime($dt." ".$tm) + - strtotime($date1);
if($diff/3600 < 24)
echo "0";
else
echo "1";

Categories