Home php How to compare time?

How to compare time?

Author

Date

Category

How to compare time in PHP? You need to know that the time entered into the form is at least 8:00 and no more than 20:00.


Answer 1

$ start_time = new datetime ($ date. $ START);
$ end_time = new datetime ($ Date. $ ED);
$ start_time_start_limit = new datetime ($ date. "08:00");
$ Start_Time_End_Limit = New DateTime ($ Date. "20:00");
$ error = '';
if ($ start_time & gt; $ end_time)
  $ error. = "End of Time Can Not Bіt Less Than The Start Time & LT; BR / & GT;";
if ($ start_time & lt; $ start_time_start_limit)
  $ error. = "START TIME CAN NOT BIT LESS 8: 00 & LT; BR / & GT;";
if ($ start_time & gt; $ start_time_end_limit)
  $ error. = "Start Time Can Not Bit More 20: 00 & lt; br / & gt;";

Answer 2

function timein ($ Time, $ min, $ max) {
  $ TIME = STRTOTIME ($ TIME);
  Return Strtotime ($ MIN) & lt; = $ Time & amp; & amp; $ Time & LT; = Strtotime ($ MAX);
}
If (Timein ($ _ POST ['Time'], '8:00', '20: 00 ')) {
  ...
}

Answer 3

as option:

/ **
   * Get the difference in the days between 2 dates
   * @Param Type $ Date1
   * @Param Type $ Date2
   * /
  PROTECTED FUNCTION DATEDIFF ($ Date1, $ DATE2)
  {
    $ datetime1 = date_create ($ DATE1);
    $ DateTime2 = Date_Create ($ DATE2);
    $ Interval = Date_Diff ($ DateTime1, $ DateTime2);
    Return $ Interval- & GT; Format ('% A');
  }

by analogy compare the clock


Answer 4

& lt;? php
  FUNCTION DATECHECK ($ DATE)
  {
    $ Date_min = New DateTime ("8:00"); // Minimum time
    $ Date_max = New DateTime ("20:00"); // Maximum time
    $ Date_Now = New DateTime ($ Date); // current time value
    // Checking whether $ Date_Now is in the range
    If ($ Date_Now & GT; = $ Date_min & Amp; & amp; $ Date_Now & LT; = $ Date_max)
    {
      RETURN TRUE;
    }
    RETURN FALSE;
  }
  if (DateCheck ("8:00"))
  {
    Print ("Everything is good.");
  } ELSE.
  {
    Print ("all bad.");
  };
? & gt;

http://ideone.com/voobr

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions