Jump to content

date format


pietbez

Recommended Posts

my date format on db used to be "26.02.2008" i have changed it to "2008.02.26" so that i can cronologicaly sort by date.

 

everything works fine exept for one of my php csripts in my calendar that reads from this table.

 

this script now gives no output at all.

 

can anyone please give me some pointers?

 

<?
include('functions.php');
$year=$_POST['year'];
$month=$_POST['month'];

$sql = "SELECT event_date FROM calendar_events WHERE status='confirmed'and event_type='gig'";
$result = mysql_query($sql);
$x=mysql_num_rows($result);
$postdata='';
if ($x>0) 
{
for ($i=0;$i<$x;++$i) {
	$row=mysql_fetch_row($result);
	$data=$row[0];
	$len=strlen($data);
	if ($len==9) $data='0'.$data;
	$monthpoint=strpos($data,'.');
	$checkmonth=substr($data,$monthpoint+1,2);
	$checkyear=substr($data,$monthpoint+4);

	if ($year==$checkyear && $month==$checkmonth)
		if ($postdata=='') $postdata=$postdata.$data; 
			else $postdata=$postdata.' '.$data;
	;}
;}
echo "resultstr=".$postdata;
?>

Link to comment
Share on other sites

hmm strange code you have. I would use this:

Check out if it works

 

<?
include('functions.php');
$year=$_POST['year'];
$month=$_POST['month'];

$sql = "SELECT event_date FROM calendar_events WHERE status='confirmed'and event_type='gig'";
$result = mysql_query($sql);
$x=mysql_num_rows($result);
$postdata='';
if ($x>0) 
{
for ($i=0;$i<$x;++$i) {
	$row=mysql_fetch_row($result);
	$fr_date = explode('.', $row[0]);
	$checkmonth=$fr_date[1];
	$checkyear=$fr_date[0];

	if ($year==$checkyear && $month==$checkmonth)
		if ($postdata=='') $postdata=$postdata.$data; 
			else $postdata=$postdata.' '.$data;
	;}
;}
echo "resultstr=".$postdata;
?>

Link to comment
Share on other sites

to be honnest, i dont know how to use date time format.

 

but now i am trying a whole new aproach.

dont laugh at my code, i am bran new at this  ;D

 

this gives me output.

 

now all i have to do is flip the dates around so it reads dd.mm.yyyy so my flash file can read it.

how do i use explode??

 

<?
include('../qazwsxedc/config.php');

mysql_connect($server,$username,$password);
@mysql_select_db($database) or die ("Unable to connect to the database");

$year=$_POST['year'];
$month=$_POST['month'];
$min="$year".'.'."$month".'.'.'00';
$max="$year".'.'."$month".'.'.'32';
$do=mysql_query ("SELECT event_date FROM calendar_events WHERE event_date>'$min' and event_date<'$max' and status='confirmed'and event_type='gig' ");
$x=mysql_num_rows($do);
if ($x>0) {
while ($row = mysql_fetch_array($do, MYSQL_ASSOC)) {
  $name.=$row["event_date"];
  $name.=' ';

}
;}
echo "resultstr=".$name;
?>

Link to comment
Share on other sites

yes, but he seems to be using a varchar for the date. pietbez, the best answer is to change the datatype of that field to DATE. More can be read on that here: http://dev.mysql.com/doc/refman/5.0/en/date-and-time-type-overview.html

 

for an easily solution working with what you have, you can use:

while ($row = mysql_fetch_array($do, MYSQL_ASSOC)) {
  list($year,$month,$day) = explode('.',$row["event_date"],3);
  $name.="$day.$month.$year ";
}

 

Link to comment
Share on other sites

to be honnest, i dont know how to use date time format.

 

but now i am trying a whole new aproach.

dont laugh at my code, i am bran new at this  ;D

 

this gives me output.

 

now all i have to do is flip the dates around so it reads dd.mm.yyyy so my flash file can read it.

how do i use explode??

 

<?
include('../qazwsxedc/config.php');

mysql_connect($server,$username,$password);
@mysql_select_db($database) or die ("Unable to connect to the database");

$year=$_POST['year'];
$month=$_POST['month'];
$min="$year".'.'."$month".'.'.'00';
$max="$year".'.'."$month".'.'.'32';
$do=mysql_query ("SELECT event_date FROM calendar_events WHERE event_date>'$min' and event_date<'$max' and status='confirmed'and event_type='gig' ");
$x=mysql_num_rows($do);
if ($x>0) {
while ($row = mysql_fetch_array($do, MYSQL_ASSOC)) {
  $name.=$row["event_date"];
  $name.=' ';

}
;}
echo "resultstr=".$name;
?>

 

explode()

 

http://ar2.php.net/manual/en/function.explode.php

 

Well with the code you provided that gave you output all you have to do is:

 

$dates = explode(".",$row["event_date"]);

 

that will give you an array $dates with month, day and year. That will look:

$dates[0] = year

$dates[1] = month

$dates[2] = day

 

you can see it doing print_r($dates); just after making an explode;

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.