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"';
?>
Related
I'm working on a project.I have to check " how many minutes ago ,user updated the database " .For that I used following code :
<?php
include('connect_db.php');
$sql =" SELECT * FROM test_table WHERE user='john' ORDER BY time_ DESC LIMIT 1" ;
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$time = strtotime($row['time_']);
$current=time();
$sub_time=$current-$time;
echo $sub_time;
}
The problem is the code is returning negative values like [ -17173 ].
The date-time stored in my db is like [ 2018-07-14 11:42:21.006515 ]
I simply want to compare current time with the the time stored in database get results in seconds or minutes .
I need help to solve this issue.Thank you .
You can just change your select statement to give you the time difference in seconds as shown below:
$sql =" select UNIX_TIMESTAMP(current_Timestamp()) - UNIX_TIMESTAMP(time_) FROM test_table WHERE user='john' ORDER BY time_ DESC LIMIT 1" ;
I think DateTime is better approach, as it has the methods to achieve the result you need.
maybe something like this:
$time=$row['time_'];
$curr_time=new DateTime();
$difference=$time->diff($curr_time);
http://php.net/manual/en/class.datetime.php
Updated
You should use the DateTime functions to figure out the difference in time. You should be able to integrate this into your code.
I incorporated my suggested code as a function into your original code. This should work for you.
Try this:
function timeDiff($date){
$date = new DateTime(date('Y-m-d H:i:s', strtotime($date)));
$current = new DateTime(date('Y-m-d H:i:s'));
$interval = $date->diff($current);
return $interval->format('%H:%I:%S');
}
include('connect_db.php');
$sql ="SELECT * FROM test_table WHERE user='john' ORDER BY time_ DESC LIMIT 1" ;
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo timeDiff($row['time_']);
}
}
You first need to convert them to timestamp. And them subtract them convert the resuting timestamp back to format you want.
while($row = $result->fetch_assoc()) {
$time = strtotime($row['time_']);
$current=time();
$sub_time=date('d-m-y h:i:s',(strototime(date('d-m-y h:i:s')-$time));
echo $sub_time;
}
You need to handle the greater than and less than cases too.
Display event name from database in current date.
This is my code:
<?php
$insum=mysql_query("SELECT * FROM `log` AS event_today WHERE `date` = $currentdate");
$d=mysql_fetch_assoc($insum);
$query = mysql_query("SELECT * FROM log");
$number=mysql_num_rows($query);
?>
<div class="inner">
<h3>
<?php
echo " {$d['event_today']} / ";
echo $number;
?></h3>
</div>
To get the date in a format that MySQL can understand (YYYY-MM-DD), use:
$currentdate = date('Y-m-d');
You also should use MySQLi rather than mysql_*(), as the old mysql_*() functions are now deprecated.
$currentdate needs to quoted, and in a yyyy-mm-dd format
So
$insum=mysql_query("SELECT * FROM `log` AS event_today WHERE `date` = '$currentdate'");
Note, this method of MySQLing has been depracated in place of PDO or mysqli
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 !
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
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.