Ali
Ali

Reputation: 1058

Show Day or Month From MySQL

i have problem to show only day or month from dob in mysql. i did these steps,

first added a table in database:

ALTER TABLE `dle_users` ADD `dob` DATE NOT NULL;

second added this code in php file:

$dob = $db->super_query( "SELECT dob FROM dle_users");
$tpl->set("{dob}", $dob);

in profile page i got correct result like this: 1372-07-08 (its a persian date)

problem is, i need to show only day or month, not full date

i tried this ways:

$dob = date("m", $row['dob']);    
$tpl->set("{dob}", $dob);

but it does not work! how i can fix this?

thank u :)

Upvotes: 0

Views: 339

Answers (2)

jmoreno
jmoreno

Reputation: 13571

If what you want from the database is the month, then use the Month function to get the month. Likewise for getting the number of days until another date.

The DB is just as capable as the front end at simple calculation, there's no reason not to do it on the DB end most of the time.

Upvotes: 1

Pragnesh Chauhan
Pragnesh Chauhan

Reputation: 8476

I don't know which framework or cms you using but you in core php should try it like this

$dob = date("m", strtotime($row['dob']));    
$tpl->set("{dob}", $dob);

and for daycounting you should check out this link

$date1 = "2007-03-24";
$date2 = "2009-06-26";

$diff = abs(strtotime($date2) - strtotime($date1));

$years = floor($diff / (365*60*60*24));
$months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
$days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));

printf("%d days\n", $days);

Upvotes: 2

Related Questions