Pagina 1 din 1

Comparare ora transmisa cu ora server

Scris: Dum Ian 14, 2018
de andras
Salut,
Am un formular care trimite la server 2 variabile: una tip data (ex.: 2018-01-14) si una tip ora (ex: ora 15).
Din aceste 2 variabile trebuie sa fac un time pe care sa-l compar cu ora server (in timp real), iar daca diferenta este mai mica de 60 minute sa blocheze actiunea user-ului (ex. INSERT).
Scopul este sa-l impiedic pe user sa se programeze in ultimul moment (mai putin de o ora). Deci, daca ora transmisa este 15, userul se poate programa la ora 14,00 dar este blocat la ora 14,01.
Cum fac asta cel mai eficient?

- Am deja functia AJAX care la blocarea userului returneaza un mesaj de alert.
Multumesc.

Comparare ora transmisa cu ora server

Scris: Dum Ian 14, 2018
de MarPlo
Salut
Poti sa folosesti in codul php functia diffDateTime() de la: marplo.net/php-mysql/diferenta-dintre-doua-date-timp-ore.html
Poate fi folosita pentru a compara si returna date dintre doua valori de tip Data si/sau Timp, in diferite formate.

- Exemplu:

Cod: SelectaĊ£i tot

<?php
/* Function that returns the difference between two date-time
 $start / $end can be in Unix Timestamp, or string containing about any English textual datetime description
 Returns an array containing a string with a textual reprezentation of the difference,
 and separately: the days, hours, minutes, seconds, total hours, total minutes, and total seconds
*/
function diffDateTime($start, $end) {
 // PHP-MySQL Course - https://coursesweb.net/php-mysql/
 // sets to use $start and $end as Unix Timestamp
 if(!is_int($start)) $start = strtotime($start);
 if(!is_int($end)) $end = strtotime($end);

 // if the difference is negative, the hours are from different days, and adds 1 day (in sec.)
 $diff = ($end >= $start) ? $end - $start : 86400 + $end - $start;

 // define the number of days, hours, minutes and seconds in difference
 $d = floor($diff / 86400);
 $h = floor(abs($diff - $d*86400)/3600);
 $m = floor(abs($diff - $d*86400 - $h*3600)/60);
 $s = $diff % 60;

 // sets the words, singular or plural
 $dstr = ($d == 1) ? ' day ' : ' days ';
 $hstr = ($h == 1) ? ' hour ' : ' hours ';
 $mstr = ($m == 1) ? ' minute ' : ' minutes ';
 $sstr = ($s == 1) ? ' second ' : ' seconds ';

 // setings for the string added in textual reprezentation of the difference
 $sdiff_d = ($d != 0) ? $d.$dstr : '';
 $sdiff_h = ($h != 0) ? $h.$hstr : '';
 $sdiff_m = ($m != 0) ? $m.$mstr : '';

 return array(
 'diff' => $sdiff_d. $sdiff_h. $sdiff_m. $s.$sstr,
 'days' => $d, 'hours'=>$h, 'min'=>$m, 'sec'=>$s,
 'totalhours' => floor($diff/3600), 'totalmin' => floor($diff/60), 'totalsec'=>$diff
 );
}

$data1 ='2018-01-14';
$hour1 =15;

// difference between a previous date-time ($data1 and $hour1) and now
$diff = diffDateTime("$data1 $hour1:00:00", 'now');

var_export($diff);
/*
array (
 'diff' => '20 minutes 28 seconds ',
 'days' => 0, 'hours' => 0, 'min' => 20, 'sec' => 28,
 'totalhours' => 0, 'totalmin' => 20, 'totalsec' => 1228
)
*/

if($diff['totalsec']<60){
  echo 'ok';
}
else {
  echo 'blocked';
}
- In caz ca te intereseaza, o functie similara, pentru comparare doua valori de tip Data/Timp in JavaScript e la:
marplo.net/javascript/diferenta-dintre-doua-date-timp-ore-js