0

Good day! I've been attempting to resolve the error specified in the title for some time now - hence why I need your help!

I know this error arises due to the PHP version I'm on being <5.4.* (I think). Unfortunately I'm no expert on PHP <5.4 (XAMPP is on >) and I've been trying to use this 'array' (as can be seen below) but still I'm having issues when putting it particular code (sourced from the internet of course) into my website.

function timetostr($timestamp) {
$age = time() - $timestamp;
if ($age == 0)
    return "just now";
$future = ($age < 0);
$age = abs($age);

$age = (int)($age / 60);        // minutes ago
if ($age == 0) return $future ? "momentarily" : "just now";

$scales = array(
    ["minute", "minutes", 60],
    ["hour", "hours", 24],
    ["day", "days", 7],
    ["week", "weeks", 4.348214286],     // average with leap year every 4 years
    ["month", "months", 12],
    ["year", "years", 10],
    ["decade", "decades", 10],
    ["century", "centuries", 1000],
    ["millenium", "millenia", PHP_INT_MAX]
);


foreach ($scales as list($singular, $plural, $factor)) {
    if ($age == 0)
        return $future
            ? "in less than 1 $singular"
            : "less than 1 $singular ago";
    if ($age == 1)
        return $future
            ? "in 1 $singular"
            : "1 $singular ago";
    if ($age < $factor)
        return $future
            ? "in $age $plural"
            : "$age $plural ago";
    $age = (int)($age / $factor);
}
}

Please note that there 'array' part used to be '['s! Any help is appreciated!

Thanks in advance!

/edit Why on earth was this marked as a duplicate if I've done what is shows there and it clearly isn't working / the only example even remotely close is for the newer version of php. I've already tried what is says there with little success, I require help.

  • 2
    And why do you have those brackets in your array, that's a syntax error ? – adeneo Dec 15 '15 at 19:22
  • Maybe the manual helps -> http://php.net/manual/en/language.types.array.php – adeneo Dec 15 '15 at 19:22
  • Sounds like you use an old php version that does not yet support short array notation. – arkascha Dec 15 '15 at 19:24
  • 1
    @adeneo That is _not_ a syntax error, it is how current php versions allow to define arrays. – arkascha Dec 15 '15 at 19:25
  • which version of php you are using ? – scaisEdge Dec 15 '15 at 19:26
  • I fully agree. This should not have been closed. It is valid PHP but version specific. I vote to reopen. – trincot Dec 15 '15 at 19:27
  • [PHP 5.4 new features](http://php.net/manual/en/migration54.new-features.php): *Short array syntax has been added,* – trincot Dec 15 '15 at 19:29
  • Replace `["minute", "minutes", 60],` by `array("minute", "minutes", 60),`, etc. – trincot Dec 15 '15 at 19:30
  • 1
    @arkascha - well, in PHP <5.4, the version the OP is using, it's a syntax error. – adeneo Dec 15 '15 at 19:30
  • http://stackoverflow.com/questions/17772534/php-array-square-brackets-and-array-difference – adeneo Dec 15 '15 at 19:31
  • Why on earth was this marked as a duplicate if I've done what is shows there and it clearly isn't working / the only example even remotely close is for the newer version of php. –  Dec 15 '15 at 19:31
  • My exact php version is 5.3.3 –  Dec 15 '15 at 19:34
  • Then it's a duplicate of this one -> http://stackoverflow.com/questions/1811100/how-to-declare-a-two-dimensional-array-most-easily-in-php – adeneo Dec 15 '15 at 19:40
  • The `[]`s should be `array()`s. – chris85 Dec 15 '15 at 19:42
  • @adeneo Ah ha! That's more like it... –  Dec 15 '15 at 19:45
  • However, I am now getting a new error: PHP Parse error: syntax error, unexpected T_LIST - I need to look into it –  Dec 15 '15 at 19:47
  • @Don I ran this to a syntax check and you have one extra curly brace. Just something to point out. I will look into this more later and tell you what I get. – RepeaterCreeper Dec 15 '15 at 19:51
  • @trincot This kind of question has already been asked many times before and was correctly closed. – Rizier123 Dec 15 '15 at 20:01
  • @Don To your list problem. [Also a version problem. You need PHP >=5.5 for it.](http://php.net/manual/en/control-structures.foreach.php#control-structures.foreach.list) – Rizier123 Dec 15 '15 at 20:02
  • @Rizier123, indeed, I found out by now, but the currently indicated duplicate is less appropriate. – trincot Dec 15 '15 at 20:17
  • @Rizier123 Thanks for that. However I can't quite seem to get it working with the 3 parameters, are you able to give me a example. –  Dec 15 '15 at 20:22
  • @Don Since you don't have PHP >=5.5 you have to do something like this: `foreach ($scales as $item) { list($singular, $plural, $factor) = $item; ...` – Rizier123 Dec 15 '15 at 20:24
  • @Rizier123 Thank you. You are an absolute star! <3 –  Dec 15 '15 at 20:31

0 Answers0