Jump to content

[SOLVED] control date and time


uisneach

Recommended Posts

Hello

I need to put a date and time check on my php script.

I absolutely need to control future dates.

I explain: I have a form with a drop down menu, and what i need is that people choose past date and time. e.g. 1st of October is good

but 1st of December, eg. , not

So, Here it is (values retrieved)

 

$day = $_POST['day'];

$month = $_POST['month'];

$year = $_POST['year'];

$time = $_POST['time'];

 

//date and time now

 

$time_1 = strtotime ("now");

 

$today_date = date("d/m/Y", $time_1);

$today_time= date("h:i:s", $time_1);

 

SO THE QUESTION is how can i control and stop the entries of future time? if the values of day month, time, etc are > than today date and time, I need to exit.

 

I hope it's clear and thx in advance to eb

 

cheers

paol:)

 

Link to comment
Share on other sites

If you convert the date and time to a timestamp, you can easily compare it to the current timestamp:

 

<?php
$day = $_POST['day'];
$month = $_POST['month'];
$year = $_POST['year'];
$time = $_POST['time'];

//may not work depending on what the above variables contain
$then = strtotime("$day $month $year $time");

if ($then > time()) {
//not okay - the specified date is in the future
} else {
//okay
}
?>

 

If it doesn't work as expected, show me an example of the $_POST variables.

Link to comment
Share on other sites

Thx

I mean, I need to check if the values are referred to a train time to report if it was late or not on a particulare day and time

Thats' why I need past time. Cause It's something that must be happened.

Nobody can report a train that is late on 1st of december!!!! (e.g)

:)

Link to comment
Share on other sites

To only compare dates:

 

<?php
$day = $_POST['day'];
$month = $_POST['month'];
$year = $_POST['year'];

$then = strtotime("$year-$month-$day");
$today = strtotime(date('Y-m-d'));

if ($then > $today) {
//not okay - the specified date is in the future
} else {
//okay
}
?>

Link to comment
Share on other sites

Hello Thebadbad

first, thx again 4 ur help

 

i am going to try your script.

sorry,i am really (a little more than a) beginner, so I am...u see..

second, it's thesame using mk time to get timestamp and compare the result with unix time?

$today = mktime (0,0,0,date("m"),date("d"), date("Y"));

Is correct ? with second, i could compare the result from mktime ?

 

bye  ;)

Link to comment
Share on other sites

  • 2 weeks later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.