Display time left to vote again - php

I got a voting script that allow people to vote for an item in the database every 24 hour and some more code to check if 24 hours have past or not using 'NOW()' and 'DATE_SUB( NOW(), 'INTERVAL 1 DAY'.
I'm currently trying to add the feature to display how much time is left before the user can vote again using the code below.
$time = explode(" ", $voteCheck[1]);
$year = explode("-", $time[0]);
$date = explode(":", $time[1]);
// Set it all to one long string
$time = $year[0].$year[1].$year[2].$date[0].$date[1].$date[2];
// Current time and date
$cTime = date("YmdHis");
// I get lost here
votcheck is an array from a sql string which just returns the first row as 0 or 1.
1 if the user already voted and 0 if the user can vote again.
The second row returns the date and time the user voted.
I just can't seem to figure out what to do to get the x amount of hours left before the user can vote again.
My main problem is that the length of the $time string sometime is 13 and sometime 14 characters long.

Ok here it is
$voted = new DateTime($time);
$daylater = date('Y-m-d H:i:s', strtotime('+1 day', $voted->format('U')));
$canvote = new DateTime($daylater);
$now = new DateTime(date("Y-m-d H:i:s"));
$diff = $canvote->format('U') - $now->format('U');
if ( $diff > 0 && $diff < 86400 ) {
$left = gmdate("H:i:s", $diff);
echo $left . ' left till you can vote again';
} else {
echo 'You can vote again' ;
}
Let me know if it works

Is it not possible to use strtotime() to convert $voteCheck[1] directly? You can then check the unix time stamp against the current time stamp.

Related

Check how many minutes crossed in a time range

How do I get the amount of worked minutes from a crossed time range?
For example:
$begindate = "00:00";
$enddate = "08:00";
if(isSaturday($begindate)){
//00:00 - 06:00 || 149%
if(strtotime($begintime) >= strtotime("00:00") and strtotime($begintime) <= strtotime("06:00")){
$weekend149 = true;
}
}
The code currently sees that the person has worked between the hours but I want to get the specific amount he has worked BETWEEN that time range only.
So if he worked from 03:00 till 08:00 I want to have a separate variable that says 3 hours or 180 minutes.
You have two time intervals and are looking for the time span of the overlap. This can be done well with DateTime. Date and time should be used for correct calculations. If only a time is specified, the calculation is made for the current date.
$startdate1 = "03:00";
$enddate1 = "08:00";
$startdate2 = "00:00";
$enddate2 = "06:00";
$startdate1 = date_create($startdate1);
$enddate1 = date_create($enddate1);
$startdate2 = date_create($startdate2);
$enddate2 = date_create($enddate2);
$startOverlap = max($startdate1,$startdate2);
$endOverlap = min($enddate1,$enddate2);
$diff = $startOverlap < $endOverlap
? $startOverlap->diff($endOverlap)
: false
;
echo $diff ? $diff->format('%h:%i:%s') : 'no overlap';
The output is limited to 24 hours. If the difference can be more than 24 hours, you have to include the days in the format.
You can test different times yourself at https://3v4l.org/Fa1sE.

php - how to echo out the next time entry I have in an array?

I'm working on a tick based space game http://ricarion.com/ but the ticks only run between certain hours.
08:00-16:30 - run every 30 minutes via a cron job. In the nav bar at the top I want to add "Next Tick: 08:30 06/02/20" for example.
So I was thinking of creating an array:
$tick_times[] = array();
$tick_times[] = 08:00;
$tick_times[] = 08:30;
$tick_times[] = 09:00;
...
$tick_times[] = 16:30;
And then this is where I get stuck, how do I check the existing time, and then compare that against the array selecting the next future time? i.e. It's now 08:34, so the return should be 09:00?
Did you need an array or just want to calculate the next 30-minute interval?
If so this may be similar to:
Round minute down to nearest quarter hour
You do modulo division of 1800 seconds on the current time and add the answer (time remainder of time to the next interval) to the then-current time to get the next event.
<?php
$current_date = date('d-M-Y g:i:s A');
echo $current_date."\n";
$current_time = strtotime($current_date);
$frac = 1800;
$r = $current_time % $frac;
$new_time = $current_time + ($frac-$r);
$new_date = date('d-M-Y g:i:s A', $new_time);
echo $new_date."\n";
http://codepad.org/xs9lMCRQ
Get the now time format it and compare it. In your case you maybe format your $tick_time to the same format like current time.
$date = new DateTime('now');
$date = $date->format('Y-m-d H:i:s');
foreach ($tick_times as $tick_time) {
$date_added = new DateTime($tick_time);
if (strtotime($date_added) == strtotime($date)) {
//do your stuff here
}
}

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.

PHP - Deduct two timestamps from each other, display in minutes

I have a mySQL database that stores the checkin and checkout time of a person in a gym. I have imported the checkin and checkout times in to my PHP script. Now I want to deduct the two timestamps from each other - giving me the time left. I want this to display in minutes.
This is my idea:
$checkOut = "2016-01-31 15:01:11";
$checkIn = "2011-01-31 15:32:35";
echo ($checkIn - $checkOut);
// I want this to display 31 minutes.
I have seen many examples on StackOverflow, but none matched my description and I couldn't reverse engineer the ones I found - because they use the time() function - which I guess takes the current time.
You can use strtotime();
$checkOut = "2016-01-31 15:01:11"; // initial value
$checkIn = "2011-01-31 15:32:35"; // initial value
$checkOut_stamp = strtotime($checkOut); // Converting to unix timestamp
$checkIn_stamp = strtotime($checkIn); // Converting to unix timestamp
echo date("i", ($checkIn - $checkOut)) . 'Minute(s)';
IMP Note: The above method will only work if the minutes are below 59, or else the hours will be rounded off and discarded. So if your requirements is showing the time in minutes which can be grater than 59 minutes eg. 144 minutes, then you'd want to just divide by 60, as follows.
$checkOut = "2016-01-31 15:01:11"; // initial value
$checkIn = "2011-01-31 15:32:35"; // initial value
$checkOut_stamp = strtotime($checkOut); // Converting to unix timestamp
$checkIn_stamp = strtotime($checkIn); // Converting to unix timestamp
$seconds = $checkOut_stamp - $checkIn_stamp;
if($seconds > 0){
if($seconds > 60){
$minutes = $seconds/60;
} else {
$minutes = 0;
}
} else {
$minutes = 0;
}
echo $minutes . ' Minute(s)';
$checkOut = "2016-01-31 15:01:11";
$checkIn = "2011-01-31 15:32:35";
$time = (strtotime($checkIn) - strtotime($checkOut));
echo date('i',$time);
use this code
If you were fetching this record from database then you can simply achieve it using MySql function TIMESTAMPDIFF, as no need to use PHP function over here
Select TIMESTAMPDIFF(MINUTE,checkIn,checkout) as tot_diff from your_table

Need To combine two columns Date and Time in mysql table and find the difference with current date and time

Actually i need to list out the data which is not exceeds 24 hours from the mail Date and Time.
I have stored the Mail Date and Mail Time in 2 different columns in database.
Please give your valid inputs from the below query I wrote,
$CTime = strtotime(date('Y-m-d H:i:s'));
//getting the 24 hours back time
$Btime = $CTime - 86400;
$listingInvoice = mysqli_query($conn, "SELECT *, (CONCAT(MailDate, MailTime)) AS MailDT FROM approved WHERE JobStatus='MailedToClient' && MailDT > '$Btime'");
Please check the date and time taken from different columns of database and find the value i.e., not exceed 24 hrs from the current time.
date_default_timezone_set('Asia/Kolkata');
//getting the current date and time...
$CTime = date('Y-m-d H:i:s');
//convert to string the current date and time...
$CSTime = strtotime(date('Y-m-d H:i:s'));
//Minus the 86400(value of 24 Hrs in seconds) in the converted date and time string
$BStime = $CSTime - 86400;
//getting the value of date and time before 24 Hrs...
$BTime = date('Y-m-d H:i:s', $BStime);
//Now the query is...
$reject = mysqli_query($conn, "SELECT * FROM approved WHERE JobStatus='MailToClient' && MailDateTime BETWEEN '$BTime' AND '$CTime'");
$row = mysqli_num_rows($reject);
if ($row == '0')
{
echo "No Jobs Mails SENT for the Last 24 Hrs";
}
else
{
....
}

Categories