1

I'm trying to display the current year using Zend Framework 2, the documentation that I've found does not cover how to output custom date formats.

Documentation can be found here: https://packages.zendframework.com/docs/latest/manual/en/modules/zend.i18n.view.helper.date.format.html?highlight=dateformat

If anyone could provide me with the syntax Zend Framework 2 syntax to output custom date/time formats it would be greatly appreciated. Thank you!

Iznogood
  • 12,447
  • 3
  • 26
  • 44

2 Answers2

3

Have a look at this question here on so.

Copy pasting the answer here: Zend_Date has been removed in favor of PHP 5.3 DateTime API http://es.php.net/manual/en/book.datetime.php

And looking at the source code for the DateFormat Helper in ZF2 it does not seem possible. Bascially just do it with the php class its adequate enough.

Zend Framework 1 or 2 does not wrap everything. When some part of php is perfect as is it is not replaced.

edit: This has worked for the question asker:

$datetime = new DateTime(null, new DateTimeZone('America/Chicago')); 
echo $datetime->format('Y');
Community
  • 1
  • 1
Iznogood
  • 12,447
  • 3
  • 26
  • 44
  • Thank you so much for your assistance! By reviewing the links you posted I was able to come up with a solution that worked in Zend Framework 2 and didn't throw any errors or warnings. Here is the final code I used: `$datetime = new DateTime(null, new DateTimeZone('America/Chicago')); echo $datetime->format('Y');` – Richard Vannauker Jan 11 '13 at 12:51
  • Next step is accepting my answer :) Happy to have been of help. – Iznogood Jan 11 '13 at 15:53
  • Iznogood apparently edited his question to get to the same answer as I have written below :) Anyhow, mind you don't need to set the timezone in this construct. Make sure you have the timezone set so you don't get any warnings about that. Don't set the timezone in your `DateTime` as it makes the code very **unflexible**. To see how you can set the timezone, review this question: http://stackoverflow.com/questions/2213608/php-configuration-it-is-not-safe-to-rely-on-the-systems-timezone-settings – Jurian Sluiman Jan 11 '13 at 18:47
  • @JurianSluiman I answered 7 hours before you pal. And What I edited in was the comment from RichVRed with the code that helped him. And anyways this is completelly legit here we strive to get the best answers possible and if a piece of someone eleses answer can help we use it. This is not a popularity contest :) And like I said look at the edit I wrote my explanation 7h before you did. please. – Iznogood Jan 11 '13 at 21:11
  • @Iznogood no need to worry. I know exactly what you wrote and that's the reason I posted my second answer, as I thought **code** will help make the answer more clear (mind the smiley, though it's small) I also commented to point out the timezone should **not** be part of the `DateTime` construction if you have not a very good reason for it. It is much better to get the timezone right server-wide or at least application-wide, as you will constantly copy/paste the timezone and get into many troubles when it changes (or you apply i18n for real) – Jurian Sluiman Jan 11 '13 at 21:39
2

Zend Framework 2 does not provide any specific date objects anymore. Instead it works with the native php DateTime. You use DateTime objects and they can be consumed in helpers to tranform the output.

Also the view helper you mention is specifically for i18n. A timestamp is formatted differently in every country, so that's what the helper is for. However, the year 2013 is 2013 in the USA, in the UK and in China. There is no need to use i18n for that.

The current year, therefore, is simply used with DateTime directly and formatted with the date() format parameters:

$date = new DateTime;

$date->format('o'); // ISO-8601 year, four digits
$date->format('Y'); // Four digits
$date->format('y'); // Two digits

You probably want either use Y or y.

Jurian Sluiman
  • 13,498
  • 3
  • 67
  • 99