Jump to content

Return Day of Week from integer in php


davidkierz

Recommended Posts

<?php
$days = array(
    1 => 'Monday',
    2 => 'Tuesday',
    3 => 'Wednesday',
    4 => 'Thursday',
    5 => 'Friday',
    6 => 'Saturday',
    7 => 'Sunday'
);

$num = 7;
echo $days[$num];
?>

is what he's suggesting, and it's certainly easier to read. I wouldn't be surprised at all if it was faster too, but I'd have to benchmark it.

The array method is a simple value lookup. The strtotime/date method does the following tasks and should be used as a last resort - parse the supplied string into hour, minute, second, month, day, year parts and put these through the mktime() function internally to get a Unix timestamp, taking into account the current time zone, then convert the resulting Unix timestamp back onto a date/time taking into account the current time zone.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.