Heather Lynn Heather Lynn - 1 year ago 89
Javascript Question

Call Javascript Date Code Into PHP Buffer

ISSUE:

I am attempting to call a JavaScript 'date_code' file (to get current YYYY/MM) to use as part of my 'directory URL' to store some .php files.

EXAMPLE:

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

NOTE:

A similar process works successfully if I am using it within a 'form submission'...

However, in this particular case, I am constructing a Cron Job, so a form will not be part of the process.

ATTEMPTS:

Among other things, I have attempted to use...

<?php $dateIndex = "path_to_date_code.js"; ?>


(as shown in the demo code below).

RESULTS:

So far... I have had no success at all being able to call in the JavaScript 'date code' or output the files to their proper destination.

SUMMARY:

If anyone could advise me what I am doing wrong or point me in the right direction, I would greatly appreciate it. Thank you in advance.

DEMO:

<?php ob_start(); ?>
<html>
<head>
</head>
<body>

<?php echo "Some Content" ?>

<?php $dateIndex = "path_to_date_code.js"; ?>

</body>
</html>

<?php echo ''; file_put_contents("../Directory/$dateIndex.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