Jump to content

Having trouble with a date function


refiking

Recommended Posts

I had a function that worked perfectly when I stored the timestamp version of time (all digits) in the db.  But, I changed it from varchar to date and have had problems with this function since.  Can you tell me what is wrong with it?  Here is the version that worked before I changed the db:

function getDatetoArray()
{
$today    = getdate();
$firstDay = mktime(0,0,0,$today['mon'],1,$today['year']);
$lastDay  = mktime(0,0,0,$today['mon']+1,0,$today['year']);

$sql = mysql_query("Select date from agenda where date>'".$firstDay."' and date<'".$lastDay."'");
$datearray = array();
while($row = mysql_fetch_array($sql))
{
	$date = getdate($row['date']);
	$datearray[] = $date['mday'];
}
return $datearray;
}

And here is the version that I am trying to fix now that doesn't work..

function getDatetoArray()
{
$today    = getdate();
$firstDay = date("Y-m-d", mktime(0,0,0,$today['mon'],1,$today['year']));
$lastDay  = date("Y-m-d", mktime(0,0,0,$today['mon']+1,0,$today['year']));

$sql = mysql_query("Select date from agenda where date>'".$firstDay."' and date<'".$lastDay."'");
$datearray = array();
while($row = mysql_fetch_array($sql))
{
	$date = time($row['date']);
	$datearray[] = $date['mday'];
}
return $datearray;
}

Link to comment
https://forums.phpfreaks.com/topic/179928-having-trouble-with-a-date-function/
Share on other sites

Ok.  So, instead of trying to convert the function, I just added another db field named date_reformatted and the function works again, but it didn't pull all of the entries like it was supposed to.  It was supposed to retrieve 3 records, but it only retrieved 2.  If I set the order to search by date or date_reformatted, the last 2 show up.  If I set it to desc, the first 2 show up.  Please help here gurus...

 

function getDatetoArray()
{
$today    = getdate();
$firstDay = mktime(0,0,0,$today['mon'],1,$today['year']);
$lastDay  = mktime(0,0,0,$today['mon']+1,0,$today['year']);

$sql = mysql_query("Select date_reformatted from agenda where date_reformatted>'".$firstDay."' and date_reformatted<'".$lastDay."'");
$datearray = array();
while($row = mysql_fetch_array($sql))
{
	$date = getdate($row['date_reformatted']);
	$datearray[] = $date['mday'];
}
return $datearray;
}

Archived

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

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