-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

6
  • 3
    Why don't you simplify it to 'Y-m-d\TH:i:s\Z' Commented Feb 11, 2021 at 14:18
  • 3
    These aren't meant to be constants, but strings. See the docs for examples. Commented Feb 11, 2021 at 14:19
  • print date('Y-m-d\TH:i:s\Z'); note that the "constants" are actually parts of the string Commented Feb 11, 2021 at 14:20
  • Just goes to show, developing with Error Reporting turned OFF is a great idea NOT Commented Feb 11, 2021 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 Commented Feb 11, 2021 at 14:24

1 Answer 1

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).

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.