How to force PHP / Apache to use another year?

So, the old project was transferred to me by another employee. It is badly coded and almost made me quit work. Twice. Because I do not have much time (I was given 2 weeks for this task), I can not rewrite all this. I changed it as I was asked, and I'm currently doing tests. The problem is that the code must change its behavior in other years. The problem with this problem is that there is no central location where the current year is set, and all the code using date("Y")that will make me change about 200 files.

So, the easiest solution would be to tell PHP at the beginning: "Hey, year 20xx." I tried date_default_timezone_set(), but that didn't help me at all. So I'm looking for:

  • PHP command that changes the current year to a given value (for example, Set_Date("Y", 2016);)
  • Apache-Server command that changes the current time (for example, setdate -Y 2016)

Is there any way, or at least a workaround, to make the script another year?

+4
source share
4 answers

You can override functions using runkit_function_redefine ()
but you need to configure runkit PECL extension

NOTE : I have not tested it!

<?php 
print date("Y");

$date = 'print "Hello, it is a new definition of date function <" . $Y . ">"; return 2014; ';
runkit_function_redefine('date', '$Y', $date);

date("Y");
+3
source

This probably isn't appropriate for your case, but I thought I should use this abuse of namespaces:

<?php

namespace FakeTime;

function date($format, $time=null){
    return 2001;
}

var_dump(date('Y'));
+2
source

Frist libfaketime https://github.com/wolfcw/libfaketime

libfaketime

if (!extension_loaded('libfaketime')) {
    dl('libfaketime.so');
}

:

auto_prepend_file, dl(.. .

PHP : https://github.com/php/php-src/blob/master/ext/date/php_date.c

+1

libfaketime. PECL.

, /etc/apache2/envvars :

export FAKETIME="2015-12-04 12:41:15"
export LD_PRELOAD=/usr/lib/faketime/libfaketime.so.1

, LD_PRELOAD git. apache2, , !

0

Source: https://habr.com/ru/post/1618585/


All Articles