Jump to content

Unix timestamp to Italian date


Manaus
 Share

Recommended Posts

I'm querying 3 pages, with this code:

<?php
function dateToItalian($unixtimestamp) {
	setlocale(LC_TIME, "it_IT.utf8");
	return ucwords(strftime("%a %d %B %Y", strtotime($unixtimestamp)));
}
?>

<?php foreach ($page->children as $child): ?>
<p>
  <?php
  echo $child->created." ";
  echo dateToItalian($child->created);
  ?>
</p>
<?php endforeach ?>

I get this:

  • 1685722140 Thu 01 January 1970
  • 1685723334 Thu 01 January 1970
  • 1662705715 Thu 01 January 1970

Anyone can explain why?

Link to comment
Share on other sites

I can see a couple of problems.

4 hours ago, Manaus said:
strtotime($unixtimestamp)

The purpose of strtotime() is to "Parse about any English textual datetime description into a Unix timestamp". But you are supplying it with a variable that is already a Unix timestamp. So that will cause strtotime() to fail and return false.

And then that false return value is then being supplied to strftime(), which instead needs to be supplied with a Unix timestamp.

Also not sure that ucwords() would be needed, as I think the date string returned by strftime() should already be capitalised correctly.

So probably it should be this:

function dateToItalian($unixtimestamp) {
	setlocale(LC_TIME, "it_IT.utf8");
	return strftime("%a %d %B %Y", $unixtimestamp);
}

 

  • Like 4
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...