I know this can be done but I am lost. I have read the manual and searched the forums for hours now. I have looked at strftime and getdate. I thought about breaking the date into an array and then redisplaying it but that seemed impossible to figure out.

Is there a quick and simple way to take the date that is in the database (stored: YYYY-DD-MM) and in the results make it display as MM-DD-YYYY?

I am trying to write a function that will take all of the dates in the database and when the results are shown in the above format.

I don't know if this will help but here is the function.

//Display Date in MM-DD-YYYY  from YYYY-DD-MM
	function displayDate($Date)
	{
		$sql = "SELECT * FROM $this->$interred, $this->$trans, $this->$request, $this->$history WHERE 
		$interred.IntermentDate = '$DateInt' AND
		$interred.DOD = '$death' AND 
		$interred.DOB = '$birth' AND 
		$interred.ServiceDate = '$service' AND 
		$request.ReqDate = '$DateReq' AND 
		$trans.InitDate = '$InitDate' AND 
		$trans.CancelDate = '$cancel' ";
		echo $sql."<br>\n";
		$result = @mysql_query($sql, $this->cnx) or die("Couldn't execute query.");
		if ($row = mysql_fetch_array($result)) {
			$dateInt= $row['IntermentDate'];
			$dateDeath= $row['DOD'];
			$dateBirth= $row['DOB'];
			$dateService= $row['ServiceDate'];
			$dateDateReq= $row['ReqDate'];
			$dateInitDate= $row['InitDate'];
			$dateCancelDate= $row['CancelDate'];
			return $Date;
		}
		return "";
	}
}

Any help is appreciated

    $original_date = '2002-31-07';
    $tmp = explode('-',$original_date);
    $new_date = $tmp[2] . '-' . $tmp[1] . '-' . $tmp[0];

    The above fragment assumes that that date is stored exactly as your post indicates i.e. YYYY-DD-MM. I think you probably meant YYYY-MM-DD.

    If it's actually stored as
    $original_date = '20023107' then
    $new_date =
    substr($original_date,6,2) . '-' .
    substr($original_date,4,2) . '-' .
    substr($original_date,0,4);

    Of course, if it's coming from a database then it might actually be in some other format completely.

      Originally posted by ahundiak
      I think you probably meant YYYY-MM-DD.

      Yes you are correct, YYYY-MM-DD

      Thank you, I will give this a try.

        Here's another function:

        function db2date($dbdate) {
        	if(strlen(trim($dbdate))) {
        		list($dbd,$dbt) = explode(' ',$dbdate);
        		list($yyyy,$mm,$dd) = explode('-',$dbd);
        		$r = "$mm/$dd/$yyyy";
        	} else {
        		$r = '';
        	}
        	return $r;
        }

        This has another couple of steps. Trims out the time, which is separated from the date by a space.

        see the line:
        $r = "$mm/$dd/$yyyy";

        change that whatever format you want.

          You could also use DATE_FORMAT() to format the date when it's pulled out of the database.

          $query = mysql_query("SELECT DATE_FORMAT(date_d, '%c %e %Y') AS date FROM yourtable WHERE id='$id'");

          Where date_d is the fieldname.

          This example would format 2002-12-13 to 12-13-2002

          If you check out the link I posted above in the manual, search that page for date_format (it's around the middle of the page) and you'll see a table with all the different formats you can use.

          Cgraz

            Write a Reply...