w3resource

PHP Date Exercises : Get the current date in Italian

PHP date: Exercise-25 with Solution

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

PHP Code Editor:

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

Previous: Write a PHP script to increment date by one month.
Next: Write a PHP script to convert the number to month name.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/php-exercises/php-date-exercise-25.php