Jump to content

Recommended Posts


The title is suppose to say date function.

 

As part of a project, I am trying to create a website that displays details of a football teams away fixtures. I have put dates of the fixtures into variables and would like to display the upcoming game.

However, It seems to only check todays date against the fixtures 'day' rather than day - month - year. This means it either loads up the wrong page or does not load any page at all.

I've put the code which i have already, does anyone know what i am doing wrong?

 

 

 

 

 

 

list($check,$data) = validate ($dbc,$_POST['email'],$_POST['pass']);

 

if ($check)

{

session_start();

 

$_SESSION[ 'user_id' ] = $data[ 'user_id' ];

$_SESSION[ 'first_name' ] = $data[ 'first_name' ];

$_SESSION[ 'last_name' ] = $data[ 'last_name' ];

$_SESSION[ 'team' ] = $data[ 'team' ];

$_SESSION[ 'email' ] = $data[ 'email' ];

 

if (($_SESSION['team']) == 'BR' )

{

date_default_timezone_set('UTC');

$fixture = date('d-m-Y');

    $fixture=date('d-m-Y', strtotime($fixture));;

 

$bury = date('d-m-Y',  strtotime("15-02-2014"));

    $scunthorpe = date('d-m-Y', strtotime("25-02-2014"));

    $northampton = date('d-m-Y',  strtotime("01-03-2014"));

    $hartlepool = date('d-m-Y',  strtotime("15-03-2014"));

$fleetwood = date('d-m-Y',  strtotime("25-03-2014"));

    $wimbledon = date('d-m-Y',  strtotime("29-04-2014"));

    $portsmouth = date('d-m-Y',  strtotime("19-04-2014"));

$wycombe = date('d-m-Y',  strtotime("28-04-2014"));

 

    if ($fixture <= $bury)           {load ('teams/bury.php');}

    elseif ($fixture <= $scunthorpe) {load ('teams/scunthorpe.php');}

elseif ($fixture <= $northampton){load ('teams/northampton.php');}

    elseif ($fixture <= $hartlepool) {load ('teams/hartlepool.php');}

elseif ($fixture <= $fleetwood)  {load ('teams/fleetwood.php');}

    elseif ($fixture <= $wimbledon)      {load ('teams/wimbledon.php');}

elseif ($fixture <= $portsmouth) {load ('teams/portsmouth.php');}

elseif ($fixture <= $wycome)   {load ('teams/portsmouth.php');}

    {echo "No remaining fixtures left for season 2013/2014";}

        }          

else

{

date_default_timezone_set('UTC');

$fixture = date('d-m-Y');

    $fixture=date('d-m-Y', strtotime($fixture));;

$orient = date ('d m Y', strtotime("11-02-2014"));

    $sheffutd = date ('d m Y', strtotime("22-02-2014"));

    $shrewsbury = date ('d m Y', strtotime("08-03-2014"));

    $peterborough = date ('d m Y', strtotime("11-03-2014"));

$colchester = date ('d m Y', strtotime("22-03-2014"));

    $rotherham = date ('d m Y', strtotime("29-03-2014"));

    $walsall = date ('d m Y', strtotime("12-04-2014"));

$stevenage = date ('d m Y', strtotime("21-04-2014"));

$crawley = date ('d m Y', strtotime("03-05-2014"));

 

    if ($fixture <= $orient)            {load ('teams/orient.html');}

elseif ($fixture <= $sheffutd)  {load ('teams/sheffutd.php');}

    elseif ($fixture <= $shrewsbury)    {load ('teams/shrewsbury.php');}

    elseif ($fixture <= $peterborough)   {load ('teams/peterbrough.php');}

elseif ($fixture <= $colchester) {load ('teams/colchester.php');}

    elseif ($fixture <= $rotherham)      {load ('teams/rotherham.php');}

elseif ($fixture <= $walsall)   {load ('teams/walsall.php');}

elseif ($fixture <= $stevenage) {load ('teams/stevenage.php');}

elseif ($fixture <= $crawley)  {load ('teams/crawley.php');}

else

{echo "No remaining fixtures left for season 2013/2014";}

 }  

 

}

Edited by Teldosh
Link to comment
https://forums.phpfreaks.com/topic/286580-data-function-not-working/
Share on other sites

in order to compare dates using greater-than/less-than, the format must be date('Y-m-d'), i.e. most-significant (year) to least-significant (day) so that the values will be compare by magnitude.

 

your date('d-m-Y') format is comparing the day first, then the month, then the year.

ok no problem. $fixture takes todays date then an if statement is ran to check if todays date is less than the upcoming date. if so, then it should load up that page. However it does not check it against 'd m Y' but just by day. Hope that helps clarify what i mean

 

date_default_timezone_set('UTC');
$fixture = date('d-m-Y');
    $fixture=date('d-m-Y', strtotime($fixture));;
 
$bury = date('d-m-Y',  strtotime("15-02-2014"));
    $scunthorpe = date('d-m-Y', strtotime("25-02-2014"));
    $northampton = date('d-m-Y',  strtotime("01-03-2014"));
    $hartlepool = date('d-m-Y',  strtotime("15-03-2014"));
$fleetwood = date('d-m-Y',  strtotime("25-03-2014"));
    $wimbledon = date('d-m-Y',  strtotime("29-04-2014"));
    $portsmouth = date('d-m-Y',  strtotime("19-04-2014"));
$wycombe = date('d-m-Y',  strtotime("28-04-2014"));
 
    if ($fixture <= $bury)           {load ('teams/bury.php');}
    elseif ($fixture <= $scunthorpe) {load ('teams/scunthorpe.php');}
elseif ($fixture <= $northampton){load ('teams/northampton.php');}
    elseif ($fixture <= $hartlepool) {load ('teams/hartlepool.php');}
elseif ($fixture <= $fleetwood)  {load ('teams/fleetwood.php');}
    elseif ($fixture <= $wimbledon)      {load ('teams/wimbledon.php');}
elseif ($fixture <= $portsmouth) {load ('teams/portsmouth.php');}
elseif ($fixture <= $wycome)   {load ('teams/portsmouth.php');}
    {echo "No remaining fixtures left for season 2013/2014";}

if you revisit this thread, a couple of words about your code. you have too much of it. you should not repeat yourself when coding. you should also separate your data from your code (i.e. make a data driven design) so that when you need to change or update the data, it's all in one place (or could even be stored in a database and retrieved.)
 

an example of how you could write the posted code -

<?php
list($check,$data) = validate ($dbc,$_POST['email'],$_POST['pass']);

if ($check)
{
    session_start();
    date_default_timezone_set('UTC');

    $_SESSION[ 'user_id' ] = $data[ 'user_id' ];
    $_SESSION[ 'first_name' ] = $data[ 'first_name' ];
    $_SESSION[ 'last_name' ] = $data[ 'last_name' ];
    $_SESSION[ 'team' ] = $data[ 'team' ];
    $_SESSION[ 'email' ] = $data[ 'email' ];

    // not sure what your team == 'BR' else really means, but define your schedule somehow as just the data needed -
    $BR['bury'] = "15-02-2014";
    $BR['scunthorpe'] = "25-02-2014";
    $BR['northampton'] = "01-03-2014";
    $BR['hartlepool'] = "15-03-2014";
    $BR['fleetwood'] = "25-03-2014";
    $BR['wimbledon'] = "29-04-2014";
    $BR['portsmouth'] = "19-04-2014";
    $BR['wycombe'] = "28-04-2014";
    
    $non_BR['orient'] = "11-02-2014";
    $non_BR['sheffutd'] = "22-02-2014";
    $non_BR['shrewsbury'] = "08-03-2014";
    $non_BR['peterborough'] = "11-03-2014";
    $non_BR['colchester'] = "22-03-2014";
    $non_BR['rotherham'] = "29-03-2014";
    $non_BR['walsall'] = "12-04-2014";
    $non_BR['stevenage'] = "21-04-2014";
    $non_BR['crawley'] = "03-05-2014";

    if ($_SESSION['team'] == 'BR' )
    {
        $schedule = $BR;
    } else {
        $schedule = $non_BR;
    }
    $fixture = date('Y-m-d');
    $found = false;
    foreach($schedule as $team=>$date){
        if($fixture <= date('Y-m-d',strtotime($date)){
            $found = true;
            load("teams/{$team}.php");
            break;
        }
    }
    if(!$found){
        echo "No remaining fixtures left for season 2013/2014";
    }
}
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.