Jump to content

Archived

This topic is now archived and is closed to further replies.

t.bo

Problem with date

Recommended Posts

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

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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">

<?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>[/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.

Share this post


Link to post
Share on other sites
Can anyone help me use the DATE_format function?

thanks in advance

Share this post


Link to post
Share on other sites
Have you read the manual page (http://www.php.net/date)?

Ken

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html

scroll down a bit and you should see DATE_FORMAT and it's usage

Share this post


Link to post
Share on other sites

×

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.