PHP

Manually Localizing Dates

If you cannot rely on setlocale(), yet want to use localized date and time values, you have to do the translations by yourself and store the results in an array. Then, you can use date() to retrieve information about a date. This serves as an index for your array.

Localizing Dates Manually
<?php
  $weekdays = array(
    'domingo', 'lunes', 'martes', 'miércoles',
    'jueves', 'viernes', 'sábado'
  );
  $months = array(
    'enero', 'febrero', 'marzo', 'abril',
    'mayo', 'junio', 'julio', 'agosto',
    'septiembre', 'octubre', 'noviembre',
       'diciembre'
  );
  $weekday = date('w');
  $month = date('n');
  echo $weekdays[$weekday] . date(', j ') .
    $months[$month - 1] . date(' Y');
?>

The preceding code does this for both the day of the month and the month itself. One array contains the Spanish weekdays; another one contains the month names.

Note that the value for the month is decreased by 1 because the array $months has no dummy element at position 0; therefore, month number one (January) has the index 0.

Using the Current Date the U.S./U.K./European Way

<?php
  echo 'US format: ' . date('m/d/Y<b\r />');
  echo 'UK format: ' . date('d/m/Y<b\r />');
  echo 'German format: ' . date('d.m.Y<b\r />');
  echo 'International format: ' . date('Y-d-m');
?>

To give you a short and convenient reference, the preceding code contains several commonly used date formats. Depending on where you are, the order in which day, month, and year are used might vary:

  • In the United States, it's (mostly) month, day, and year

  • In the United Kingdom and the rest of Europe, it's (mostly) day, month, and year

  • The international standard date notation starts with the year and continues with month and day

The preceding code used a four-digit representation of the year because this is nonambiguous. In practice, however, two-digit years are also commonly used.

by BrainBellupdated
Advertisement: