-2

I am using some of the available constants in PHP to show the date and time in a particular format:

$created_date=date(Y.'-'.m.'-'.d.'\T'.H.':'.i.':'.s.'\Z',$created_timestamp);

Since updating from PHP 7.1 to 7.4, I get the following messages in the log file:

PHP Warning: Use of undefined constant Y - assumed 'Y' (this will throw an Error in a future version of PHP)

PHP Warning: Use of undefined constant m - assumed 'm' (this will throw an Error in a future version of PHP)

PHP Warning: Use of undefined constant d - assumed 'd' (this will throw an Error in a future version of PHP)

PHP Warning: Use of undefined constant H - assumed 'H' (this will throw an Error in a future version of PHP)

PHP Warning: Use of undefined constant i - assumed 'i' (this will throw an Error in a future version of PHP) PHP Warning: Use of undefined constant s - assumed 's' (this will throw an Error in a future version of PHP)

I can't seem to find any documentation (or ideally) examples of how I could update my code to get rid of this problem.

Thanks for any help

Martin
  • 22,212
  • 11
  • 70
  • 132
David Robertson
  • 479
  • 7
  • 17
  • 3
    Why don't you simplify it to `'Y-m-d\TH:i:s\Z'` – Nigel Ren Feb 11 '21 at 14:18
  • 3
    These aren't meant to be constants, but strings. See [the docs](https://www.php.net/manual/en/datetime.format.php) for examples. – Alex Howansky Feb 11 '21 at 14:19
  • `print date('Y-m-d\TH:i:s\Z');` note that the "constants" are actually parts of the string – Martin Feb 11 '21 at 14:20
  • Just goes to show, developing with Error Reporting turned OFF is a great idea NOT – RiggsFolly Feb 11 '21 at 14:22
  • "assumed 'Y'" is your hint. `date(y.'-'.m.'-'.d)` is assumed as: `date('y'.'-'.'m'.'-'.'d')`->`date('y-m-d')`. The code in the Q is wrong but works becasue of a quirck. The error is becasue the quirck no longer works – Martijn Feb 11 '21 at 14:24
  • ___NOTE___ This has given very similiar errors all the way back to PHP5.6 and probably further, but thats a far back as I have VERSIONS to test it. bUT EVAL GOES ALL THE WAY https://3v4l.org/2SlbH – RiggsFolly Feb 11 '21 at 14:30

1 Answers1

5

This change is not from 7.2 - it's probably just that you have a different error reporting setting now. The values you're attempting to use aren't constants in PHP at all, but when you've had a different error reporting setting you haven't gotten the warning about them not being constants - and they've just been used as strings instead.

The correct way to use date is to just send the formatting string in directly:

$created_date = date('Y-m-d\TH:i:s\Z', $created_timestamp);

If you're trying to create an iso8601 timestamp, you can use the DATE_ATOM constant as well (it'll probably use +00:00 instead of Z).

MatsLindh
  • 49,529
  • 4
  • 53
  • 84