chocopi Posted May 20, 2007 Share Posted May 20, 2007 ok so in my db i have dates stored like dd/mm/yyyy. how can i split the string up to get date = dd month = mm and year = yy. This is so i can then get an array on the month so it can show dd [month name] yyyy Thanks, ~ Chocopi Quote Link to comment https://forums.phpfreaks.com/topic/52248-solved-strings/ Share on other sites More sharing options...
Barand Posted May 20, 2007 Share Posted May 20, 2007 If you stored your dates correctly as yyyy-mm-dd in a DATE field you could either use Mysql to get month name SELECT MONTHNAME(datecol) as mname ... or use PHP to get the month name $mname = date('F', strtotime($datecol)) Quote Link to comment https://forums.phpfreaks.com/topic/52248-solved-strings/#findComment-257775 Share on other sites More sharing options...
chocopi Posted May 20, 2007 Author Share Posted May 20, 2007 Thanks, i shall change it to yyyy-mm-dd Quote Link to comment https://forums.phpfreaks.com/topic/52248-solved-strings/#findComment-257777 Share on other sites More sharing options...
Barand Posted May 20, 2007 Share Posted May 20, 2007 In addition, you will now be able to sort by date and compare dates, to see which is later. You can't do either with dd/mm/yyyy. Quote Link to comment https://forums.phpfreaks.com/topic/52248-solved-strings/#findComment-257778 Share on other sites More sharing options...
chocopi Posted May 20, 2007 Author Share Posted May 20, 2007 Cheers thanks, but, what will the whole Mysql line look like ? ~ Chocopi Quote Link to comment https://forums.phpfreaks.com/topic/52248-solved-strings/#findComment-257779 Share on other sites More sharing options...
Barand Posted May 20, 2007 Share Posted May 20, 2007 you can either <?php $sql = "SELECT DATE_FORMAT(datecol, '%d %M %Y') as date FROM mytable"; $res = mysql_query($sql) or die (mysql_error()."<p>$sql</p>"); while ($row = mysql_fetch_assoc($res)) { echo $row['date'], '<br>'; } ?> or, formatting the date with php <?php $sql = "SELECT datecol FROM mytable"; $res = mysql_query($sql) or die (mysql_error()."<p>$sql</p>"); while ($row = mysql_fetch_assoc($res)) { echo date('d F Y', strtotime($row['datecol'])), '<br>'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/52248-solved-strings/#findComment-257781 Share on other sites More sharing options...
chocopi Posted May 20, 2007 Author Share Posted May 20, 2007 Cheers Barand, im using the top one and tis super. Thanks, ~ Chocopi Quote Link to comment https://forums.phpfreaks.com/topic/52248-solved-strings/#findComment-257784 Share on other sites More sharing options...
chocopi Posted May 20, 2007 Author Share Posted May 20, 2007 Just one last point, how do you minus dates for example: reg_date = 2007-05-17 last_login = 2007-05-20 last login - reg_date = 3 You have been registered for 3 days something like that anyways. Thanks, ~ Chocopi Quote Link to comment https://forums.phpfreaks.com/topic/52248-solved-strings/#findComment-257789 Share on other sites More sharing options...
Barand Posted May 20, 2007 Share Posted May 20, 2007 SELECT DATEDIFF(last_login, reg_date) as daysreg Quote Link to comment https://forums.phpfreaks.com/topic/52248-solved-strings/#findComment-257800 Share on other sites More sharing options...
chocopi Posted May 20, 2007 Author Share Posted May 20, 2007 ive added it like this but it doesnt work: <?php $age = "SELECT DATEDIFF(last_login, reg_date) as daysreg"; while ($row = mysql_fetch_assoc($age)) { echo $row['daysreg']; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/52248-solved-strings/#findComment-257811 Share on other sites More sharing options...
chigley Posted May 20, 2007 Share Posted May 20, 2007 <?php $age = mysql_query("SELECT DATEDIFF(last_login, reg_date) as daysreg"); while ($row = mysql_fetch_assoc($age)) { echo $row['daysreg']; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/52248-solved-strings/#findComment-257813 Share on other sites More sharing options...
chocopi Posted May 20, 2007 Author Share Posted May 20, 2007 cheers, but its still not working Quote Link to comment https://forums.phpfreaks.com/topic/52248-solved-strings/#findComment-257822 Share on other sites More sharing options...
chigley Posted May 20, 2007 Share Posted May 20, 2007 <?php $age = mysql_query("SELECT DATEDIFF(last_login, reg_date) as daysreg FROM tablename"); while ($row = mysql_fetch_assoc($age)) { echo $row['daysreg']; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/52248-solved-strings/#findComment-257824 Share on other sites More sharing options...
chocopi Posted May 20, 2007 Author Share Posted May 20, 2007 cheers, but it is still coming up blank Quote Link to comment https://forums.phpfreaks.com/topic/52248-solved-strings/#findComment-257826 Share on other sites More sharing options...
chigley Posted May 20, 2007 Share Posted May 20, 2007 <?php $age = mysql_query("SELECT DATEDIFF(last_login, reg_date) as daysreg FROM tablename") or die(mysql_error()); while ($row = mysql_fetch_assoc($age)) { print_r($row); } ?> What does that output? Quote Link to comment https://forums.phpfreaks.com/topic/52248-solved-strings/#findComment-257827 Share on other sites More sharing options...
chocopi Posted May 20, 2007 Author Share Posted May 20, 2007 it outputs: You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near '(last_login, reg_date) as daysreg FROM Mark' at line 1 Quote Link to comment https://forums.phpfreaks.com/topic/52248-solved-strings/#findComment-257828 Share on other sites More sharing options...
Barand Posted May 20, 2007 Share Posted May 20, 2007 DATEDIFF requires MySQL 4.1.1 or later. If you are using an earlier version try SELECT (TO_DAYS(last_login) - TO_DAYS(reg_date)) as daysreg FROM Mark Quote Link to comment https://forums.phpfreaks.com/topic/52248-solved-strings/#findComment-257830 Share on other sites More sharing options...
chocopi Posted May 20, 2007 Author Share Posted May 20, 2007 Cheers Barand, thats solved it THANKS ~ Chocopi Quote Link to comment https://forums.phpfreaks.com/topic/52248-solved-strings/#findComment-257833 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.