Visualizzazione dei risultati da 1 a 5 su 5
  1. #1

    dov'è l'errore? (se c'è)

    ho trovato in rete uno script che dovrebbe calcolare la differenza tra due date, una preimpostata e quella corrente, l'ho inserito in una pagina ma non mi funziona.
    mi sapete dire dove è l'errore se c'è?
    codice:
    <?php
    // This is a simple script to calculate the difference between two dates
    // and express it in years, months and days
    // 
    // use as in: "my daughter is 4 years, 2 month and 17 days old" ... :-)
    //
    // Feel free to use this script for whatever you want
    // 
    // version 0.1 / 2002-10-3
    //
    // please send comments and feedback to webmaster@lotekk.net
    //
    
    // ****************************************************************************
    
    // configure the base date here
    $base_day		= 20;		// no leading "0"
    $base_mon		= 8;		// no leading "0"
    $base_yr		= 2005;		// use 4 digit years!
    
    // get the current date (today) -- change this if you need a fixed date
    $current_day		= date ("j");
    $current_mon		= date ("n");
    $current_yr		= date ("Y");
    
    // and now .... calculate the difference! :-)
    
    // overflow is always caused by max days of $base_mon
    // so we need to know how many days $base_mon had
    $base_mon_max		= date ("t",mktime (0,0,0,$base_mon,$base_day,$base_yr));
    
    // days left till the end of that month
    $base_day_diff 		= $base_mon_max - $base_day;
    
    // month left till end of that year
    // substract one to handle overflow correctly
    $base_mon_diff 		= 12 - $base_mon - 1;
    
    // start on jan 1st of the next year
    $start_day		= 1;
    $start_mon		= 1;
    $start_yr		= $base_yr + 1;
    
    // difference to that 1st of jan
    $day_diff	= ($current_day - $start_day) + 1; 	// add today
    $mon_diff	= ($current_mon - $start_mon) + 1;	// add current month
    $yr_diff	= ($current_yr - $start_yr);
    
    // and add the rest of $base_yr
    $day_diff	= $day_diff + $base_day_diff;
    $mon_diff	= $mon_diff + $base_mon_diff;
    
    // handle overflow of days
    if ($day_diff >= $base_mon_max)
    {
    	$day_diff = $day_diff - $base_mon_max;
    	$mon_diff = $mon_diff + 1;
    }
    
    // handle overflow of years
    if ($mon_diff >= 12)
    {
    	$mon_diff = $mon_diff - 12;
    	$yr_diff = $yr_diff + 1;
    }
    
    // the results are here:
    
    // $yr_diff  	--> the years between the two dates
    // $mon_diff 	--> the month between the two dates
    // $day_diff 	--> the days between the two dates
    
    // ****************************************************************************
    
     /*simple output of the results 
    print "The difference between ".$base_yr."-".$base_mon."-".$base_day." ";
    print "and ".$current_yr."-".$current_mon."-".$current_day." is:";
    print "
    
    ";
    
    // this is just to make it look nicer
    $years = "years";
    $days = "days";
    if ($yr_diff == "1") $years = "year";
    if ($day_diff == "1") $days = "day";
    
    // here we go
    print $yr_diff." ".$years.", ";
    print $mon_diff." month and ";
    print $day_diff." ".$days;
    */
    ?>

  2. #2
    Utente di HTML.it
    Registrato dal
    Mar 2004
    Messaggi
    1,141
    io l'ho provato e mi funziona:
    Codice PHP:
    <?php
    // This is a simple script to calculate the difference between two dates
    // and express it in years, months and days
    // 
    // use as in: "my daughter is 4 years, 2 month and 17 days old" ... :-)
    //
    // Feel free to use this script for whatever you want
    // 
    // version 0.1 / 2002-10-3
    //
    // please send comments and feedback to [email]webmaster@lotekk.net[/email]
    //

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

    // configure the base date here
    $base_day        1;        // no leading "0"
    $base_mon        1;        // no leading "0"
    $base_yr        2005;        // use 4 digit years!

    // get the current date (today) -- change this if you need a fixed date
    $current_day        date ("j");
    $current_mon        date ("n");
    $current_yr        date ("Y");

    // and now .... calculate the difference! :-)

    // overflow is always caused by max days of $base_mon
    // so we need to know how many days $base_mon had
    $base_mon_max        date ("t",mktime (0,0,0,$base_mon,$base_day,$base_yr));

    // days left till the end of that month
    $base_day_diff         $base_mon_max $base_day;

    // month left till end of that year
    // substract one to handle overflow correctly
    $base_mon_diff         12 $base_mon 1;

    // start on jan 1st of the next year
    $start_day        1;
    $start_mon        1;
    $start_yr        $base_yr 1;

    // difference to that 1st of jan
    $day_diff    = ($current_day $start_day) + 1;     // add today
    $mon_diff    = ($current_mon $start_mon) + 1;    // add current month
    $yr_diff    = ($current_yr $start_yr);

    // and add the rest of $base_yr
    $day_diff    $day_diff $base_day_diff;
    $mon_diff    $mon_diff $base_mon_diff;

    // handle overflow of days
    if ($day_diff >= $base_mon_max)
    {
        
    $day_diff $day_diff $base_mon_max;
        
    $mon_diff $mon_diff 1;
    }

    // handle overflow of years
    if ($mon_diff >= 12)
    {
        
    $mon_diff $mon_diff 12;
        
    $yr_diff $yr_diff 1;
    }

    // the results are here:

    // $yr_diff      --> the years between the two dates
    // $mon_diff     --> the month between the two dates
    // $day_diff     --> the days between the two dates

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

     //simple output of the results 
    print "The difference between [b]".$base_yr."-".$base_mon."-".$base_day."[/b] ";
    print 
    "and [b]".$current_yr."-".$current_mon."-".$current_day."[/b] is:";
    print 
    "

    "
    ;

    // this is just to make it look nicer
    $years "years";
    $days "days";
    if (
    $yr_diff == "1"$years "year";
    if (
    $day_diff == "1"$days "day";

    // here we go
    print $yr_diff." ".$years.", ";
    print 
    $mon_diff." month and ";
    print 
    $day_diff." ".$days;

    ?>
    Output a video:
    codice:
    The difference between 2005-1-1 and 2006-1-3 is:
    
    1 year, 0 month and 2 days
    ciao

  3. #3
    grazie ken!
    in effetti c'erano alcuni blocchi commentati male in quello che avevo provato...

    un'ultima cosa:

    come potrei modificarlo in modo che, qualora la variabile anni risulti uguale a 0, nella stampata a video del risultato mi ometta il valore degli anni?

  4. #4
    Utente di HTML.it
    Registrato dal
    Mar 2004
    Messaggi
    1,141
    ti scrivo solo il pezzo interessato:
    Codice PHP:
    // here we go
    if($yr_diff 0)
     print 
    $yr_diff." ".$years.", ";

    print 
    $mon_diff." month and ";
    print 
    $day_diff." ".$days;

    ?> 
    non l'ho provata, comunque dovrebbe stamparti la scritta
    "n years" solo se n > 0
    Prova,nel caso fosse sbagliata chiedo venia...

    ciao

  5. #5
    Originariamente inviato da ken84
    ti scrivo solo il pezzo interessato:
    Codice PHP:
    // here we go
    if($yr_diff 0)
     print 
    $yr_diff." ".$years.", ";

    print 
    $mon_diff." month and ";
    print 
    $day_diff." ".$days;

    ?> 
    non l'ho provata, comunque dovrebbe stamparti la scritta
    "n years" solo se n > 0
    Prova,nel caso fosse sbagliata chiedo venia...

    ciao
    conviene <> infatti se fosse negativo..

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.