Jump to content

Day of the month conversion


Drummin

Recommended Posts

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

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')";

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.