Himani
Himani

Reputation: 265

PHP Fatal Error Call to a member function format() on boolean

Fatal: Call to a member function format() on boolean

How can I show the date in current language?

$date = 1496102399;
$date = DateTime::createFromFormat('j F Y',"@$date");
$date = $date->format('Y-m-d');

I want the date to be translatable when using this date format: j F Y How can I achieve this?

Upvotes: 1

Views: 1797

Answers (2)

Harshal Shah
Harshal Shah

Reputation: 427

WordPress has inbuilt function date_i18n() to get the date in localized format.

You can try below code:

echo date_i18n("d F Y (H:i:s)", strtotime('2017-05-27 16:08:01')) ;

Upvotes: 0

Sahil Gulati
Sahil Gulati

Reputation: 15141

Do it like this, there are few problems in your code.

Problems:

1. $end_date is not initialized. just changing this will also not work. check here

2. DateTime::createFromFormat expects second parameter to be time string (like this 15-Feb-2009) not timestamp.

Try this code snippet here

ini_set('display_errors', 1);

$unixtimestamp = 1496102399;
$date = new DateTime();
$date->setTimestamp($unixtimestamp);
echo $end_date = $date->format('Y-m-d');

For displaying the date in Deutsch language you can use this, prerequisites are listed below of this code doesn't work.

<?php

ini_set('display_errors', 1);
$loc=setlocale(LC_ALL,'de_DE');
echo strftime('%d %B %Y',1496102399);

1. locale -a Listing all locale's present on your system, make sure de_DE,de_DE.iso88591,de_DE.utf8 these locale's are present on your system

2. locale-gen de_DE

3. dpkg-reconfigure locales reconfigure locales

Upvotes: 2

Related Questions