wordpress - PHP use timestamp as a constant ← (PHP)

Just starting with wordpress and PHP especially, though I have plenty of experience with other non-web-dev languages. I'm attempting to use a plugin to create a folder with a timestamp, then any file that gets uploaded on that page should use the same timestamp's folder, instead of having one file per timestamp.

to help with that, I'm trying to get the timestamp of the beginning of my program and store that through the lifetime of the program so that I can use it in different places.

$time = date("h:i:s");

if (!function_exists('wfu_before_file_check_handler')) {
    
    function wfu_before_file_check_handler($changable_data, $additional_data, $time){
        $changable_data["file_path"] = str_replace("%time%", $time, $changable_data["file_path"]);
        return $changable_data;
    }
    add_filter('wfu_before_file_check', 'wfu_before_file_check_handler', 10, 2); 
}

works to give me the date, of course, but because of PHP's scope limitations, I can't just access this variable in a function. I can't get it to work as a global, and I can't define it as a constant. At least I haven't been able to do those things yet; that doesn't necessarily mean I can't.

Answer



Solution:

If you're asking how to define a constant, try this:

define('APP_TIMESTAMP', date('h:i:s'));

Then you can use it in your code like this:

str_replace('%time%', APP_TIMESTAMP, $changeable_data)

There will be no scope limitations

Source