I would like to use the functions ‘date_add()’ and ‘date_sub()’ but my host server only has PHP version 5.2.17, these functions need 5.3+
Are there any alternatives for older versions I could use?
If not, what is the best way to do this?
What I want to do is display different tables from MySQL according to date. So one will show data dated from a month ago until now, another from now to a month ahead and a third showing from a month ahead onwards.
For example the query for the first table is something like this:
$askSQL = "SELECT * FROM mytable WHERE date BETWEEN '$monago' AND '$now' ORDER BY date" ;
Where $monago is the date one month ago and $now is todays date. Whats the best way to set the variable $monago?
I’m thinking I may need to convert to Unix Timestamps and do a bit of maths on that then convert back to ‘Y-m-d’ which SQL understands, is that the right way?
Well you can simply convert each date string into a timestamp integer and apply addition/subtraction from the two timestamps, then convert it back to a new date string. This way it should work even if you dont use the date addition/subtraction methods.
Also I have to say that PHP 5.2 is way too outdated, and if your webhost does not provide PHP 5.3 or above its not worth sticking with. My advice is for you to change your webhost before its too late, as you may as well get into problems that cannot be solved without using PHP 5.3 features. My own software now requires PHP 5.3+ minimum and theres no compatibility support for PHP 5.2, I am sure lots of other web applications work this way nowadays.
What I want to do is display different tables from MySQL according to date. So one will show data dated from a month ago until now, another from now to a month ahead and a third showing from a month ahead onwards.
The first query should be:
$askSQL = "SELECT * FROM mytable WHERE date BETWEEN DATE_SUB(NOW(),INTERVAL 1 MONTH) AND NOW() ORDER BY date" ;
The second query should be:
$askSQL = "SELECT * FROM mytable WHERE date BETWEEN NOW() AND DATE_ADD(NOW(),INTERVAL 1 MONTH) ORDER BY date" ;
and the third query should be:
$askSQL = "SELECT * FROM mytable WHERE date > DATE_ADD(NOW(),INTERVAL 1 MONTH) ORDER BY date" ;
No date manipulation in PHP is required as it can all be handled by the SELECT statement itself. So the PHP version doesn’t matter as long as it supports sql calls of some sort.
These also have the advantage that because they don’t use any PHP variables there is no possibility of SQL injection.
Thanks all for the answers.
I did get this working using the Unix Timestamp method, but I may change it now to use the SQL DATE_ADD and DATE_SUB functions instead, they seem a more elegant solution. I was not aware of these, I’m fairly new at SQL and PHP, so there are probably loads of functions I don’t know about.
As for the old PHP version, i have fallen foul of this before, when I have looked up a function for what I want to do, then think “Why is this not working?”, only to find it’s too new for my version.
I will write to my host and see if they will upgrade, if not I will consider moving.
It seems I could switch to PHP 5.4 all along, there is a thing called PHP Config in my cPanel that does it, so I could do that myself. I’m now on PHP 5.4.2. just had to set the proper timezone in the new php.ini and I’m away.
But now it get’s interesting, things are not working due to depreciated functions, so now I have to weed all those out and find new equivelents.
The first stumbling block is session_register, so i’m back to searching for alternatives, but this time new for old rather than old for new.
But at least I have a modern PHP version now and new functions available.
Deprecated means “obsolete now and will be gone in the next version so don’t use it in any new code and fix anywhere that you have used it as soon as possible so your code will be ready for the next version”.
It is only with HTML and JavaScript where so many people who don’t know what deprecated means and so keep using deprecated code that has meant that it has become impossible to complete the deprecation by removing them from the next version as doing what everyone was told 15 years ago was going to happen would break 90% of the web because people continued to use deprecated code…
I already changed a few depreciated functions, eg, preg_replace for reg_replace and suchlike. But I did not know session_register was depreciated until I updated the PHP version and got an error. The manual says this one is already removed.
My script was based on a tutorial that is maybe a bit dated.