t.bo Posted July 24, 2006 Share Posted July 24, 2006 Hi everybody,I want to get the month, year and date out of my complete date (when enterred).Code : [code]<?$dat = 2006-07-24;$year = substr($dat,0,2);$month = substr($dat,3,2);$day = substr($dat,6,2);echo "year is $year<br>";echo "month is $month<br>";echo "day is $day<br>"?>[/code]It displays incorrect numbers.What im I doing wrong?Grtz and thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/15471-problem-with-date/ Share on other sites More sharing options...
hackerkts Posted July 24, 2006 Share Posted July 24, 2006 You can try to use explode();[code]<?php$dat = "2006-07-24";$date = explode("-", $dat);echo "year is ". $date[0] ." <br>";echo "month is ". $date[1] ." <br>";echo "day is ". $date[2] ." <br>";?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/15471-problem-with-date/#findComment-62744 Share on other sites More sharing options...
kenrbnsn Posted July 24, 2006 Share Posted July 24, 2006 Or[code]<?php$dt = "2006-07-04";list($year,$month,$day) = explode('-',$dt);echo "year is $year<br>";echo "month is $month<br>";echo "day is $day";?>[/code]or[code]<?php$dt = "2006-07-24";echo date('\y\e\a\r \i\s Y\<\b\r\>\m\o\n\t\h \i\s F\<\b\r>\d\a\y \i\s d',strtotime($dt));?>[/code]Ken Quote Link to comment https://forums.phpfreaks.com/topic/15471-problem-with-date/#findComment-62763 Share on other sites More sharing options...
t.bo Posted July 24, 2006 Author Share Posted July 24, 2006 Ok thanks guys, helped me a lot.Now ... I'm making a calendar. The user uploads an event with title and date (ex. 2006-07-24). Then the title of the current month should output as a title and then the events ordered by date below.This is my code : [code]<?php$currentmonth = date("m");?><h1>Agenda</h1><table width="100%" border="1" cellspacing="0" cellpadding="0"><?phpinclude('dbconnect.php');$sql = mysql_query("select * from agenda where monthfield = '$currentmonth'") or die(mysql_error());while($row = mysql_fetch_array($sql)){ $event = $row["eventfield"]; $id = $row["idfield"]; $url = $row["urlfield"]; $date = $row["date"]; $month = $row["monthfield"]; echo "<tr><td><h2>$month</h2></td></tr>"; echo "<tr><td><a href='$url'>$event</a></td>"; echo "<td>$date<td></tr>";}?></table>[/code]1.) I find this code kinda stupid because the user has to enter the number of the month twice. So im looking for the code that the month can be selected from the main date formfield. (2006-[color=green]07[/color]-24) and that that number can be used to output only current month events. I read that the function DATE_FORMAT has to be used?2.) My current code does not work like it should because it still displays every month and not only the current month. Quote Link to comment https://forums.phpfreaks.com/topic/15471-problem-with-date/#findComment-62768 Share on other sites More sharing options...
t.bo Posted July 24, 2006 Author Share Posted July 24, 2006 Can anyone help me use the DATE_format function?thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/15471-problem-with-date/#findComment-62780 Share on other sites More sharing options...
kenrbnsn Posted July 24, 2006 Share Posted July 24, 2006 Have you read the manual page (http://www.php.net/date)? Ken Quote Link to comment https://forums.phpfreaks.com/topic/15471-problem-with-date/#findComment-62854 Share on other sites More sharing options...
t.bo Posted July 24, 2006 Author Share Posted July 24, 2006 Thats help for php date() function and sorts.. But i need date stuff in MySQL.On mysql.com its a little too much and way to many functions... Quote Link to comment https://forums.phpfreaks.com/topic/15471-problem-with-date/#findComment-63060 Share on other sites More sharing options...
ryanlwh Posted July 24, 2006 Share Posted July 24, 2006 http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.htmlscroll down a bit and you should see DATE_FORMAT and it's usage Quote Link to comment https://forums.phpfreaks.com/topic/15471-problem-with-date/#findComment-63064 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.