php mysql finding rows older than a day from date feald - php

Here is how it should be:
search the database for uploads older than a day.
output the id's for the uploads in a list.
here is what I have so far:
<?php
include 'config.php';
$connect = mysql_connect(DB_HOST, DB_USER, DB_PASS);
mysql_select_db(DB_NAME);
$timestamp = strtotime("-1 days");
$efined = mysql_query("SELECT * FROM uploads WHERE timestamp < '$timestamp'");
$efound = mysql_query($efined);
$enum = mysql_numrows($efound);
$ecount = 0;
echo $enum.'records were found.';
while ($ecount < $enum) {
$euid = mysql_result($efound,$ecount,"uid");
echo $euid.'<br>';
$ecount++;
}
mysql_close($connect);
?>
currently, this outputs nothing, when there is a record 3 days old.
How would I spesify the date format? in my database, it looks like this: 2013-04-02.
thanks for any help,
josh.

this
$enum=mysql_numrows($efound);
should be
$enum=mysql_num_rows($efound);
EDIT.
try your sql like that
where timestamp < date('now', '-1 days')
edit:
you are defining two mysql_query
change this
$efined = mysql_query("SELECT * FROM uploads WHERE timestamp < '$timestamp'");
to
$efined = "SELECT * FROM uploads WHERE timestamp < '$timestamp'";

Your mistake...
$efined = mysql_query("SELECT * FROM uploads WHERE timestamp < '$timestamp'");
$efound=mysql_query($efined);
$enum=mysql_numrows($efound);
There must be only one query and wrong num_rows name...
$sql= "SELECT * FROM uploads WHERE timestamp < '$timestamp'";
$efound = mysql_query($sql);
$enum = mysql_num_rows($efound);
P.S. The old myslq functions support ends at PHP 5.4 . Its good for you to start using myslqi or PDO mysql !

Related

How to select a timestamp using date in mysql and php

I want to query if there is a particular timestamp for a day using the distinct dates that are present in a table and so far I have come up with this code
$mysqli = new mysqli('localhost', 'root', 'rolemodel', 'dashboard');
$dates = "SELECT DISTINCT DATE_FORMAT(mytimestamp, '%Y-%m-%d') FROM ".$this->table_name." ORDER BY DATE(mytimestamp) ASC;";
/* check connection */
if ($mysqli->connect_errno) {
printf("Connect failed: %s\n", $mysqli->connect_error);
exit();
}
$rowsdata = $mysqli->query($dates); // get number of rows
$num_rows_date = $rowsdata->num_rows;
for ($i = 0; $i<=$num_rows_date; i++) {
$current_query = "SELECT mytimestamp FROM ".$this->table_name." WHERE mytimestamp =".$rowsdata[$i]; //not sure on what to use here
}
I am confused on how to query the exact timestamp which is supposed to be the particular day along with the 00:00:00
Example 2014-06-02 00:00:00
Hope my question makes sense.
try:
//$rowsdata = $mysqli->query($dates); // get number of rows
//$num_rows_date = $rowsdata->num_rows;
//for ($i = 0; $i<=$num_rows_date; i++) {
//This is usually the way you want to do this with plain old arrays
if($result = $mysqli->query($dates)){
while($row = $result->fetch_row()){
$current_query = "SELECT mytimestamp FROM ".$this->table_name." WHERE DATE(mytimestamp) ='".$row[0] . "'";
//do something awesome here ...
}
}
See the docs for the DATE function.
BUT ... it looks like you're just querying the same table you pulled the dates from in the first place, so you're guaranteed to find timestamps with those dates. Not sure what you're ultimately after, but you may be able to do it with a single query.

Why won't this SQL statement take a string (mm/dd/yy) and convert it into days?

In my database, I have varchar values that signify dates - however I cannot input them into the database as dates. So, I am trying to pull them from the database and use them to calculate days.
Format in table:
mm/dd/yy
My SQL Statement (in PHP):
$conn = oci_connect("user", "pass", "(description=(address=(protocol=tcp)(host=host)(port=1533))(connect_data=(service_name=sid)))");
$sel = "select RECEIVED from INTOXDM.JOINT_USE where RECEIVED is not null";
$par = oci_parse($conn, $sel);
$exe = oci_execute($par);
$fetch = oci_fetch_all($par,$array);
echo $fetch;
$arraynum = 0;
while ($array) {
$arraynum = $arraynum + 1;
$date = $array[RECEIVED][$arraynum];
$today=date("m/d/y");
$now=strtotime($today); // or your date as well
$your_date=strtotime("$date");
$DAYS=($now - $your_date)/(60*60*24);
$upd = "update INTOXDM.JOINT_USE set DAYS = '$DAYS' where RECEIVED = '$date'";
$pars = oci_parse($conn, $upd);
$exe = oci_execute($pars); }
This code seems to work for some of the varchars, however many others are passed as a ridiculous value (16k days). This is because the date is being saved as 12/30/1969, I believe. This signifies that I have a problem with my date coding, but I cannot seem to figure it out.
Perhaps you should do the calculation in Oracle?
select trunc(sysdate - to_date(RECEIVED, 'MM/DD/YY'))
from INTOXDM.JOINT_USE
where RECEIVED is not null;
You can even do the update this way as well:
update INTOXDM.JOINT_USE
set DAYS = trunc(sysdate - to_date(RECEIVED, 'MM/DD/YY'))
where RECEIVED is not null;
The moral of the story is that dates should be stored in databases using the native date/time formats.

get a date from a mysql query in php

I have a mysql database.
I have a php script, that connects to my database. My database consists of 3 tables, error, date, email address, as per the screenshot
my php query is as follows:
<?php
mysql_connect("localhost", "username", "password") or die(mysql_error());
mysql_select_db("pmta_reporting") or die(mysql_error());
$result = mysql_query("SELECT * FROM microcompany WHERE date='2013-01-28' AND code='2.0.0'") or die(mysql_error());
$count= mysql_num_rows($result);
echo $count;
mysqli_close($con);
?>
I would like to replace '2013-01-28' with yesterdays date and in that format Y-m-d.
How can I do this?
Any feedback would be appreciated
$yesterday = date("Y-m-d", strtotime('-1 day'));
$result = mysql_query("SELECT * FROM microcompany WHERE date='$yesterday' AND code='2.0.0'") or die(mysql_error());
Why not use the MySQL function? They are meant to be used in MySQL.
SELECT * FROM microcompany WHERE date=DATE(DATE_SUB(NOW(), INTERVAL 1 DAY)) AND code='2.0.0'
btw. mysql-* functions are deprecated: http://www.php.net/manual/en/function.mysql-query.php
$date = new DateTime('Yesterday');
echo $date->format('Y-m-d');
Or
$date = new DateTime('2013-01-28');
$date->modify('-1 day');
echo $date->format('Y-m-d');
FYI, you shouldn't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO, or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.
use native mysql functions for that purpose.
$result = mysql_query("SELECT * FROM microcompany WHERE
date=date_sub(date(now()), interval 1 day) AND code='2.0.0'")
or die(mysql_error());
$date = new DateTime('Yesterday');
$fdate = $date->format('Y-m-d');
echo $fdate;
<?php
$yesterday = date('Y-m-d', strtotime('-1 day',strtotime(date("y-m-d")) ));
$query = "SELECT * FROM microcompany WHERE date='".$yesterday."' AND code='2.0.0'";
$newQuery = 'update microcompany set date = "'.$yesterday.' where date = "'.$oldDate.'" and code ="2.0.0"';
?>

Matching date and time from mysql - logic issue

I have uploaded a PHP script in the server. What it does is, that it keeps reading the DB (mySQL), and if the DATE_OF_MATCH and TIME_OF_MATCH (these are 2 fields in the mySQL db) is equal to the server time it will execute a message.
fields in the table: all are VARCHAR
ID, DATE_OF_MATCH, TIME_OF_MATCH, MATCH_NAME
One record from the MATCH table;
1 , 1/12/2012, 3:40, ManU vs Kaks
The problem is that, my select statement is wrong. My $theDateAndTime is returning 09:15:03PM and in the Database i am having 2 separate records for date and time. So how can i edit the select statement so could match the date and time against the $theDateAndTime (returned by the server)
The code:
<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("my_db", $con);
date_default_timezone_set('America/New_York');
$theDateAndTime = date("h:i:sA")."\n";
$result = mysql_query("SELECT * FROM MATCH where DATE_OF_MATCH=".$theDateAndTime." and TIME_OF_MATCH=".$theDateAndTime."");
while(true){
if(result!=null){
while($row = mysql_fetch_array($result))
{
echo $row['MATCH_NAME'] ;
echo "<br />";
}
}
}
mysql_close($con);
?>
I don't understand your question very well, but seems that you are trying to query for date and time in two different fields but you are only take the current time. I think you should try something like this:
<?php
$current_date = date('d/m/Y');
$current_time = date('h:i');
$result = mysql_query("SELECT * FROM MATCH where DATE_OF_MATCH=".$current_date." and TIME_OF_MATCH=".$current_time."");
Otherwise, you should not use mysql_* functions, use mysqli_* functions instead with prepared statements.
Create two separate variables for date and then time.
$the_date = date("n/j/Y");
$the_time = date("h:i:s");
$result = mysql_query("SELECT * FROM MATCH where DATE_OF_MATCH={$the_date} and TIME_OF_MATCH={$the_time}");
http://us3.php.net/manual/en/function.date.php

strtotime php functions outputs good time in php file but wrong after update in MySQL

Ive got such script:
mysql_connect('localhost', 'xxx', 'xxx');
mysql_select_db('xxx');
$sql = "SELECT * FROM rl_cronjobs";
$sql = mysql_query($sql);
$row = mysql_fetch_array($sql);
$sunday = $row['next_sunday'];
$today = date('Y-m-d');
if($sunday == $today){
$sql = "DELETE * FROM xxx WHERE stala != 1";
$sql = mysql_query($sql);
echo $nextsunday;
$nextsunday = strtotime("next Sunday");
$nextsunday = date('Y-m-d', $nextsunday);
$sql = "UPDATE xxx SET next_sunday = $nextsunday";
$sql = mysql_query($sql) or die (mysql_error());
echo $nextsunday;
}
And in MySQL the value of it is: 1984
I need it to be normal date of next sunday
The type of MySQL table row is varchar because when i set tu date it doesn't update. Any help?
Don't store dates in varchar fields. Mysql has date, datetime, and timestamp fields for such things, and using them opens a world of new possibilities. E.g. your "next sunday" calculations can be done as simply as:
UPDATE xxx SET next_sunday = NOW() + INTERVAL (8 - DAYOFWEEK(NOW())) DAY
without ever involving the PHP time functions. Note that this particular function will always pick the real next sunday, so if you run this code on a Sunday, you'll get the following weeks' sunday, not "today", e.g.:
'2012-12-08' (Saturday) -> '2012-12-09'
'2012-12-09' (Sunday) -> '2012-12-16'
'2012-12-10' (Monday) -> '2012-12-16'
As well, note that you're trying to echo $nextsunday before it's been defined.
Looks like you missing some quotes " or ' in your update query. You probably meant to write:
$sql = "UPDATE xxx SET next_sunday = '$nextsunday'";
sidenote: The myslq_* functions are deprecated, you shouldn't use them in new code anymore.

Categories