Jump to content


[SOLVED] PHP Date Disaster


4 replies to this topic

#1 Jackdawhouse

    Young Padawan

  • Members
  • Pip
  • 11 posts

Posted 27 March 2008 - 04:59 PM

How can I see if the posted time has past the real time?

EG:

<?php
$time = date("h:i"); // real time
$ntime = $_POST['ntime']; // posted time
##
# if $ntime is 23:00 and $time is 22:00, show page
header("Location: ....");
##
# if $ntime is 22:00 and $time is 23:00, don't show page
header("Location: ....");
?>

Thanks.

Edited by Jackdawhouse, 29 March 2008 - 12:21 AM.


#2 BigDog

    Young Padawan

  • Members
  • Pip
  • 277 posts
  • Gender:Male
  • Location:Orange County, California
  • Interests:Running, building computers, PC games and BMX and programming.

Posted 27 March 2008 - 06:54 PM

Well, instead of using date, you can use time()
so make sure that POST['ntime'] is in time() format and do this:
$time = time(); //returns real time
$ntime = $_POST['ntime'];

if($ntime < $time)
echo "Time has not passed";
else if($ntime > $time)
echo "You in the future!";


#3 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 27 March 2008 - 11:30 PM

If the $_POST['ntime'] is in a human readable time (i.e. not the result of time()), use strtotime() to convert it to a comparable value.

#4 The Creator

    Young Padawan

  • Members
  • Pip
  • 115 posts
  • Gender:Male
  • Location:England
  • Interests:Computers, Music, Technology, Sport

Posted 28 March 2008 - 07:58 AM

Try this bad boy:

<?php
$hours = date("h"); // hours
$minutes = date("i"); // minutes

$ntime = $_POST['ntime']; // posted time

$nbreak = explode(':', $nbreak);

$nhours = $nbreak[0];
$nminutes = $nbreak[1];

if($nhours >= $hours && $nminutes > $minutes){
header("Location: ..."); // show page
} else {
header("Location: ..."); // dont show page
}

?>

That will work for what you want, but there are several things wrong with (logically).

1. It isn't taking into account AM or PM.

2. It isn't taking into account what day it is, what month it is, or what year it is.

3. The page you want to be shown can be visited even if the posted time isn't past the actual time.

To take into account days and AM or PM you'll want something like this:


<?php
$hours = date("h"); // hours
$minutes = date("i"); // minutes
$meridian = date("a"); // am or pm
$day = date("j"); // day of month
$month = date("n"); // month
$year = date("Y"); // year

$ntime = $_POST['ntime']; // posted time


/******* YOU WILL NEED TO POST NEW VARIABLES WITH THE DAY, MONTH, MERIDIAN AND YEAR*******/

$nday = $_POST['nday'];
$nmonth = $_POST['nmonth'];
$nyear = $_POST['nyear'];
$nmeridian = $_POST['nmeridian'];

/********************************************************************************
******/

$nbreak = explode(':', $nbreak);

$nhours = $nbreak[0];
$nminutes = $nbreak[1];

if($nmonth == $month && $nday >= $day){
$monthCheck = true;
} else {
if($nmonth > $month){
$monthCheck = true;
} else {
$monthCheck = false;
}
}

if($nyear == $year && $nmonth >= $month){
$yearCheck = true;
} else {
if($nyear > $year){
$yearCheck = true;
} else {
$yearCheck = false;
}
}

// if ndate and the actual date are the same
if($nmonth == $month && $nyear == $year && $nday == $day){

if($meridian == 'pm' && $nmeridian == 'am'){
$meridianCheck = true;
} else {
$meridianCheck = false;
}


} else {

if($nmonth > $month && $nyear > $year && $nday > $day){
$meridianCheck = true;
} else {
$meridianCheck = false;
}

}


if($nhours >= $hours && $nminutes > $minutes && $monthCheck == true && $yearCheck == true && $meridianCheck == true){
header("Location: ..."); // show page
} else {
header("Location: ..."); // dont show page
}

?>

Might be a bit more than you need, and there could be a mistake or too, i havent tested it. And i can already see things i should of done to cut down the code :biggrin: . I also have a bad feeling that theres a function which returns an integer value of the year, month, day and time all in one. Ah well, it's there if you want it ;)

The Creator

#5 Jackdawhouse

    Young Padawan

  • Members
  • Pip
  • 11 posts

Posted 29 March 2008 - 12:19 AM

Thank-you all for your help! The problem is solved.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users