Heather Lynn Heather Lynn - 1 year ago 97
PHP Question

Using Date Variable As File Path


Thanks to RockwoodON and Josan for their help! :)

OBJECTIVE: I am attempting to use a 'date' code (YYYY/MM) as part of a 'directory URL' to store some .php files.

EXAMPLE: "../Directory/2017/07.php" would represent the .php 'directory' file for the month of July, 2017.

The following php code worked great!

<?php ob_start(); ?>

<?php echo "Some Content" ?>

<?php $date = date("Y/m");?>


<?php echo ''; file_put_contents("$date.php", ob_get_contents()); ?>

Answer Source

You are confusing the roles of PHP and Javascript. PHP runs on the server, while Javascript and your date code file only run IN THE BROWSER. The Javascript will do nothing on the server.

I don't really understand what you are trying to do, but how about constructing a path like this:

$date = date("Y/m");  // Y is 4 digit year, m is two digit month 
$path = "../Directory/{$date}.php";

The braces are not really needed. They are used to isolate a variable when embedded in a string. I've added them so the variable stands out.

If you need to make a path for a given date, use this type of thing:

$date_to_use = "2015-12-12"; // whatever
$date = date("Y/m", strtotime($date_to_use));  
$path = "../Directory/{$date}.php";
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download