Jump to content

getting dates from database


corbeeresearch

Recommended Posts

Hi,

 

I'm trying to get the date, using this code

 

<?php
include('config.php');
$today = date('Y-m-d 00:00:00');
$nineyesterday = date('Y-m-d H:i:s', mktime(date("H") - (date("H") + 6), date("i") - date("i"), date("s") - date("s"), date("m") , date("d"), date("Y")));
$now = date('Y-m-d H:i:s');
$nineam = date('Y-m-d 9:00:00');
$ninepm = date('Y-m-d 18:00:00');

if ((strtotime($now) >= strtotime($today)) &&  (strtotime($now) <= strtotime($nineam)))
{
	//count the answers by yes or no
	$q = "SELECT opt, count(opt) FROM plus_poll_ans WHERE date<='".$nineyesterday."' AND date>='".$nineam."' GROUP BY opt desc "; 
}
elseif ((strtotime($now) >= strtotime($nineam)) &&  (strtotime($now) <= strtotime($ninepm)))
{
	//count the answers by yes or no
	$q = "SELECT opt, count(opt) FROM plus_poll_ans WHERE date<'".$nineam."' AND date>='".$ninepm."' GROUP BY opt desc "; 
}
	$q = mysql_query($q) or die(mysql_error());

	//separate the results
while($row = mysql_fetch_array($q))
{
	$total_opt[] = $row['count(opt)'];
}

mysql_freeresult($q);

$yes = intval($total_opt[0]);
$no = intval($total_opt[1]);

$total = $yes + $no;
        echo $total."<br/>";
        echo $yes."<br/>";
        echo $no."<br/>";
?>

 

but it didn't work, why isn't it working?

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/218115-getting-dates-from-database/
Share on other sites

You're right. I fixed the code, unfortunately, I got no query results

 

<?php
include('config.php');
$today = date('Y-m-d 00:00:00');
$nineyesterday = date('Y-m-d H:i:s', mktime(date("H") - (date("H") + 6), date("i") - date("i"), date("s") - date("s"), date("m") , date("d"), date("Y")));
$now = date('Y-m-d H:i:s');
$nineam = date('Y-m-d 9:00:00');
$ninepm = date('Y-m-d 18:00:00');
echo '9pm yesterday: '.$nineyesterday;
echo '<br/>';
echo '9am: '.$nineam;
echo '<br/>';
echo 'now: '.$now;
$dif= strtotime($nineam) - strtotime($now);
echo '<br/>';
echo $dif;
echo '<br/>';

if ((strtotime($now) >= strtotime($today)) &&  (strtotime($now) <= strtotime($nineam)))
{
	//count the answers by yes or no
	$q = "SELECT opt, count(opt) FROM plus_poll_ans WHERE date>'".$nineyesterday."' AND date<'".$nineam."' GROUP BY opt desc "; 
}
elseif ((strtotime($now) >= strtotime($nineam)) &&  (strtotime($now) < strtotime($ninepm)))
{
	//count the answers by yes or no
	$q = "SELECT opt, count(opt) FROM plus_poll_ans WHERE date<'".$nineam."' AND date>='".$ninepm."' GROUP BY opt desc "; 
}
	$q = mysql_query($q) or die(mysql_error());

	//separate the results
while($row = mysql_fetch_array($q))
{
	$total_opt[] = $row['count(opt)'];
}

mysql_freeresult($q);

$yes = intval($total_opt[0]);
$no = intval($total_opt[1]);

$total = $yes + $no;
?>

 

my output is

9pm yesterday: 2010-11-07 18:00:00
9am: 2010-11-08 9:00:00
now: 2010-11-08 22:57:12
-50232
Query was empty

 

I don't understand:

 

when I do this query in phpmyadmin,

"SELECT opt ,date FROM `plus_poll_ans` WHERE date BETWEEN '2010-11-07 18:00:00' AND '2010-11-08 09:00:00' "

 

I get result:

Yes 2010-11-07 22:49:52

 

but when I do it in php

if ((strtotime($now) > strtotime($nineyesterday)) &&  (strtotime($now) < strtotime($nineam)))
{
	//count the answers by yes or no
	$q = "SELECT opt, count(opt) FROM plus_poll_ans WHERE date BETWEEN '".$nineyesterday."' AND '".$nineam."' GROUP BY opt desc "; 
}
elseif ((strtotime($now) > strtotime($nineam)) &&  (strtotime($now) < strtotime($ninepm)))
{
	//count the answers by yes or no
	$q = "SELECT opt, count(opt) FROM plus_poll_ans WHERE date BETWEEN '".$nineam."' AND '".$ninepm."' GROUP BY opt desc "; 
}
	$q = mysql_query($q) or die(mysql_error());

 

I get nothing

 

 

I found that the cause is the if statement, but I'm not sure what to replace with it

if ((strtotime($now) >= strtotime($today)) &&  (strtotime($now) <= strtotime($nineam)))
{

}
elseif ((strtotime($now) >= strtotime($nineam)) &&  (strtotime($now) <= strtotime($ninepm)))
{

}

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.