Global Variable

Hi.

I am using code along these lines:

    $start = date_create(1990-10-01);
    $end = new DateTime();
    $diff = date_diff($start, $end);
    $trading = $diff->format("%y")

to calculate how long a company has been trading. Works well.

I am using a PHP framework which separates framework (generic) code from project-specific code. I would like to adjust this code so that it exists within the framework code and all I need in the project-specific code is a value I can code once (start_date) which I can send to this code (in a function say) and I need the result to be available easily from every page in the site (not just a master page but individual pages) which I can include with a short and simple span.

How to do this?

I’m guessing I put this code inside a function and replace the hard-coded start_date with a passed in value from the project specific side and have it return the value???

Not sure how to store the variable to pass in once? Constant?

Not sure how I would then call/use it a span?

Help

I’m not sure if others would think this is a good practice, but I like to keep a helper function file as part of my framework which can be loaded as needed. Then I can just call a function from there when needed. I use this helper file to store functions that I use often, such as date and other kinds of formatting.

1 Like

Your guess would be correct, IMO.

1 Like

Most PHP Frameworks always call index.php and if and only if this is the case then try adding this script to index.php.

I would first copy and paste index.php to index-test.php first and test the script in the new file:

<?php declare(strict_types=1);
error_reporting(-1);
ini_set('display_errors', '1');

echo "<!doctype html><html lang='en'>
<head> <title> Test Date Reset </title> </head>
<body>
<h1> Connect-Learning.tk </h1>
";

  if( isset($_GET['password']) && isset($_GET['date']) ):
    if('my-super-dooper-password' === $_GET['password'] ) :
      $start = date_create( $_GET['date'] );  
    endif;
  endif;

  if( isset($start) ) :
    // DATE ALREADY SET 
  else:  
    $start = date_create('1990-10-01');
  endif;  

  $end      = new DateTime( '2020-05-18' );
  $diff     = date_diff($start, $end);
  $trading  = $diff->format("%y");

  $TEST = '?password=my-super-dooper-password&date=2010-05-17';
  echo "<h2>
          <a href='$TEST'> Test </a>
          &nbsp; &nbsp;
          <a href='?'> Reset </a>
        </h2>
        ";  

  if( isset($_GET['date']) ):
    echo '<h4>' .$TEST .'</h4>' ;   

    echo '<h2> DEBUG </h2>';

    fred($diff,   '$diff', ! true);
    fred( $_GET, '$_GET');
    fred($start,  '$start', ! true);
    fred($end,    '$end', ! true);
  endif;  

  echo '</body></html>';


  //=================================================
  function fred($val, $title='', $vd=false)
  {
      echo '<hr><pre><b>' .$title .' ==> </b>';
        if($vd) :
          var_dump($val);
        else:  
          print_r($val);
        endif;  
      echo '</pre></hr>';
  }

Online Demo

1 Like

Thank you John.

That looks like exactly what I’m after.

1 Like

Version 002 would be to include this script in a separated file:

<?php declare(strict_tyLee=1);

if( isset($_GET['password']) && isset($_GET['date']) ):
    if('my-super-dooper-password' === $_GET['password'] ) :
      $start = date_create( $_GET['date'] );  
    endif;
  endif;

Or hardcode the following:
'$start = date_create( ‘2011-12-25’ );`