-1

I'm trying to effectively echo the date every 1 second following a sleep(1) However I get the error

Cannot redeclare date();

Here is my function:

function date()
{
  echo date('d/m/Y - G:i a');
  sleep(1);
}

then on the page I've just got

<?php date();?>

What am I doing wrong ?

Stephen R
  • 3,512
  • 1
  • 28
  • 45
Chris Y
  • 21
  • 7
  • 1
    It's as the error message states, you cannot re-declare functions. `date` is a language level (i.e. built-in) function. – Jonnix Dec 03 '18 at 13:27
  • have changed the function name to showdate and it does now display the date however its not doing it every 1 second as specified – Chris Y Dec 03 '18 at 13:28
  • "as specified"...specified where? You've shown no code which would cause the function to be called repeatedly...and if you want to do that, you'll need to do it client-side, not server-side, unless you want your page to refresh every second (and I don't think your users will thank you for that). – ADyson Dec 03 '18 at 13:29
  • ahh yeah good point.. just switched it over to jquery and its working as planned now many thanks – Chris Y Dec 03 '18 at 13:34

1 Answers1

1

A function named date() already exists, built in to PHP - see http://php.net/manual/en/function.date.php

Therefore you can't re-use the name. You just need to give your function a different name (or place it within a class or namespace, and reference it as such).

ADyson
  • 57,178
  • 14
  • 51
  • 63