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
https://forums.phpfreaks.com/topic/130642-solved-control-date-and-time/
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.

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)

:)

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
}
?>

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  ;)

Then use a variation of my first script :)

 

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

$then = strtotime("$year-$month-$day $time");

if ($then > time()) {
//specified date is in the future
exit;
}
?>

  • 2 weeks later...

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.