Drummin Posted March 12, 2012 Share Posted March 12, 2012 Hello, Doing a little calendar where days are incremented as variable $i formatted like 'j' or single digit numbers 1,2,3 etc. (The calendar works fine) However for querying DB tables I need the day formatted as 'd' or 01,02,03 etc. so on any day I need to make this conversion. I can't find an easy solution though there must be one. Thanks in advance. <?php //test day $i=7; $day=date('d', mktime($i)); echo "$day"; //Should be 07 ?> Link to comment https://forums.phpfreaks.com/topic/258757-day-of-the-month-conversion/ Share on other sites More sharing options...
cpd Posted March 12, 2012 Share Posted March 12, 2012 echo str_pad($day, 2, 0, STR_PAD_LEFT); Link to comment https://forums.phpfreaks.com/topic/258757-day-of-the-month-conversion/#findComment-1326496 Share on other sites More sharing options...
Drummin Posted March 12, 2012 Author Share Posted March 12, 2012 That seems to work great, Thanks. Link to comment https://forums.phpfreaks.com/topic/258757-day-of-the-month-conversion/#findComment-1326497 Share on other sites More sharing options...
Pikachu2000 Posted March 12, 2012 Share Posted March 12, 2012 You could do it in the query string rather simply also with MySQL's STR_TO_DATE() function. This will accept the days/months with with or without the leading zeros. $date = '2010-2-9'; // OR $date = '2010-02-09'; $query = "SELECT field FROM table WHERE date_field = STR_TO_DATE('$date', '%Y-%c-%e')"; Link to comment https://forums.phpfreaks.com/topic/258757-day-of-the-month-conversion/#findComment-1326503 Share on other sites More sharing options...
Drummin Posted March 12, 2012 Author Share Posted March 12, 2012 Oh, that's very cool as well. Thanks for your help. Link to comment https://forums.phpfreaks.com/topic/258757-day-of-the-month-conversion/#findComment-1326504 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.