davidkierz Posted November 1, 2008 Share Posted November 1, 2008 sorry guys i been messing around with a this stupid program and im gettin tired. isnt there a builtin function that will return the day of week via a supplied integer from 1 - 7 1 = monday 2 = tuesday etc.etc... Link to comment https://forums.phpfreaks.com/topic/131031-return-day-of-week-from-integer-in-php/ Share on other sites More sharing options...
genericnumber1 Posted November 1, 2008 Share Posted November 1, 2008 There might be a better way, but $num = 7; echo date('l', strtotime("Sunday + $num Days")); works. Link to comment https://forums.phpfreaks.com/topic/131031-return-day-of-week-from-integer-in-php/#findComment-680288 Share on other sites More sharing options...
PFMaBiSmAd Posted November 1, 2008 Share Posted November 1, 2008 Just make an array where the index is the day number and the value is the day name. Link to comment https://forums.phpfreaks.com/topic/131031-return-day-of-week-from-integer-in-php/#findComment-680289 Share on other sites More sharing options...
genericnumber1 Posted November 1, 2008 Share Posted November 1, 2008 <?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. Link to comment https://forums.phpfreaks.com/topic/131031-return-day-of-week-from-integer-in-php/#findComment-680290 Share on other sites More sharing options...
davidkierz Posted November 2, 2008 Author Share Posted November 2, 2008 yeah thats what i was gonna use i just thought there was a built in function in php that did it already... Link to comment https://forums.phpfreaks.com/topic/131031-return-day-of-week-from-integer-in-php/#findComment-680292 Share on other sites More sharing options...
genericnumber1 Posted November 2, 2008 Share Posted November 2, 2008 Nope, nothing built in. And his method is MUCH faster as I expected. date() and strtotime() are both very slow, especially compared to straight variables. Link to comment https://forums.phpfreaks.com/topic/131031-return-day-of-week-from-integer-in-php/#findComment-680294 Share on other sites More sharing options...
PFMaBiSmAd Posted November 2, 2008 Share Posted November 2, 2008 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. Link to comment https://forums.phpfreaks.com/topic/131031-return-day-of-week-from-integer-in-php/#findComment-680300 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.