w3resource

PHP Date Exercises : Get the current date in Italian


25. Current Date in Italian

Write a PHP script to get the current date in Italian.

Sample Solution:

PHP Code:

<?php
// Set the locale to Italian and store the previous locale
$old_Locale = setlocale(LC_TIME, 'it_IT');

// Get the current date and time formatted according to the Italian locale
// The strftime function is used to format the date and time according to the specified format
echo strftime("Today is %a on %b %d, %Y")."\n";

// Restore the previous locale
setlocale(LC_TIME, $old_Locale);
?>

Output:

Today is Tue on Jul 11, 2017 

N.B.: The result may varry for your system date and time.

Explanation:

In the exercise above,

  • $old_Locale = setlocale(LC_TIME, 'it_IT');: Sets the locale to Italian (it_IT) for date and time functions and stores the previous locale in the variable '$old_Locale'.
  • echo strftime("Today is %a on %b %d, %Y")."\n";: Uses the "strftime()" function to format the current date and time according to the Italian locale, using the specified format string. The format string '%a' represents the abbreviated weekday name, '%b' represents the abbreviated month name, '%d' represents the day of the month, and '%Y' represents the year in four digits.
  • setlocale(LC_TIME, $old_Locale);: Restores the previous locale.

Flowchart :

Flowchart: Get the current date in Italian

For more Practice: Solve these Related Problems:

  • Write a PHP script to display the current date in Italian by mapping month and day names to their Italian equivalents.
  • Write a PHP function to translate the current date into Italian and output it with custom formatting.
  • Write a PHP program to fetch the current date, convert the month to Italian using an associative array, and output the full formatted date.
  • Write a PHP script to dynamically generate and display a date string in Italian, ensuring proper accentuation and capitalization.

Go to:


PREV : Increment Date by One Month.
NEXT : Convert Number to Month Name.

PHP Code Editor:



Have another way to solve this solution? Contribute your code (and comments) through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.