Jump to content


Photo

Problem with date


  • Please log in to reply
7 replies to this topic

#1 t.bo

t.bo
  • Members
  • PipPipPip
  • Advanced Member
  • 57 posts

Posted 24 July 2006 - 11:24 AM

Hi everybody,

I want to get the month, year and date out of my complete date (when enterred).

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>"

?>

It displays incorrect numbers.
What im I doing wrong?

Grtz and thanks in advance

#2 hackerkts

hackerkts
  • Members
  • PipPipPip
  • Advanced Member
  • 593 posts
  • LocationSingapore
  • Age:18

Posted 24 July 2006 - 11:29 AM

You can try to use explode();

<?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>";
?>


Regards,
hackerkts

To be a coder, you must learn how to think and not to give up so easily.


#3 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 24 July 2006 - 11:58 AM

Or
<?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";
?>
or
<?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));
?>

Ken

#4 t.bo

t.bo
  • Members
  • PipPipPip
  • Advanced Member
  • 57 posts

Posted 24 July 2006 - 12:09 PM

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 :
<?php
$currentmonth = date("m");

?>
<h1>Agenda</h1>

<table width="100%"  border="1" cellspacing="0" cellpadding="0">

<?php
include('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>

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-07-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.

#5 t.bo

t.bo
  • Members
  • PipPipPip
  • Advanced Member
  • 57 posts

Posted 24 July 2006 - 12:44 PM

Can anyone help me use the DATE_format function?

thanks in advance

#6 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 24 July 2006 - 02:09 PM

Have you read the manual page (http://www.php.net/date)?

Ken

#7 t.bo

t.bo
  • Members
  • PipPipPip
  • Advanced Member
  • 57 posts

Posted 24 July 2006 - 06:45 PM

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...

#8 ryanlwh

ryanlwh
  • Staff Alumni
  • Advanced Member
  • 511 posts

Posted 24 July 2006 - 06:48 PM

http://dev.mysql.com...-functions.html

scroll down a bit and you should see DATE_FORMAT and it's usage
Please use EDIT * 100...
Please use
or [php] * 1000...

PLEASE READ THE POSTED SOLUTIONS CAREFULLY * 1000000...




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users