I'm trying to develop a simple php based schedule ...
This is my database table:
Let's say that current time/date is: 2012-03-21 02:00:00
PHP script should echo: Meeting 2
I have a part of the script, but no idea what to do next
<?php
$con = mysql_connect("localhost","root","");
if (!$con){die('Could not connect: ' . mysql_error());}
mysql_select_db("schedule", $con);
$current_time = date("Y-m-d H:i:s");
$result = mysql_query("SELECT * FROM events");
while($row = mysql_fetch_array($result)){
echo $row['Subject'];
}
mysql_close($con);
?>
How to filter query, to mach current time and scheduled subject? Thank you!
(if current_time is between StartTime and EndTime - echo it's Subject)
Try this query
SELECT * FROM events WHERE NOW() BETWEEN StartTime AND EndTime
Try select * from events where StartTime <= NOW() and EndTime >= NOW()
NOW() will give you current time in mysql.
Please try the following query
select * from events where current_time between startTime and endTime
Your query should like this
$result = mysql_query("SELECT * FROM events WHERE current_time between startTime and endTime);
The mysql BETWEEN operator may be of some use.
$startDate = gmdate('Y/m/d/ H:i:s','2012-03-21 02:00:00');
$endDate = gmdate('Y/m/d/ H:i:s','2012-03-21 02:23:59');
$sql = 'select subject from events
where startTime between "'.gmdate("Y/m/d H:i:s", $startDate) .
'" and "' . gmdate("Y/m/d H:i:s", $endDate).'") ';
Firstly, you're going to have to convert your StartTime and EndTime into a timestamp using strtotime so you can compare it to the current time (same thing goes for current time):
http://php.net/manual/en/function.strtotime.php
Then just say if current_time > StartTime AND current_time < EndTime echo Subject.
Something like this should work:
while($row = mysql_fetch_array($result)){
$startTime = strtotime($row['StartTime']);
$endTime = strtotime($row['StartTime']);
if (strtotime($current_time) > $startTime && strtotime($current_time) < $endTime
echo $row['Subject'];
}
putenv("TZ=Asia/Calcutta"); // To Set TimeZone
$now = date("Y-m-d H:i:s");
$sql = "select * from events where '$now' between StartTime and EndTime";
Related
OK! So, I have a page where I'm trying to pull a list of records from the database with dates < 30 days from expiration.
Now, the tricky part is - that the date format is in "yyyy-mm-dd hh:mm:ss"
Here's my code:
$Today = date("Y-m-d h:i:s");
if ($result = $mysqli->query("SELECT * FROM table WHERE DATEDIFF(ExpireDate, '$Today') < 30 ORDER BY `StartDate` ASC"))
{
while($row = mysqli_fetch_array($result))
{
//show records
//this type of query usually works when I'm NOT doing stuff with dates
}
mysqli_close($mysqli);
}
When I try to pull just records without any date/time conditions, it gives me the entire list of records, but when I want to see the those conditions, it doesn't display anything.
How do I show the records that are 30 days from expiration?
I SHOULD NOTE - that this is for internal use, only, and that there is no access from the outside world.
(I just wanted something simple for internal records keeping)
I've even tried this:
select * from table where some_date < curdate() - interval 30 day
and failed miserably... thoughts?
Just let the > & < operators take care of it
$Today = date("Y-m-d h:i:s");
$ExpiresFromToday= date("Y-m-d h:i:s", strtotime("+30 days"));
if ($result = $mysqli->query("SELECT * FROM `table` WHERE (`ExpireDate` < '$ExpiresFromToday' AND `ExpireDate` > '$Today') ORDER BY `StartDate` ASC")){
while($row = mysqli_fetch_array($result)){
//show records
}
mysqli_close($mysqli);
}
EDIT
to work with DATEDIFF tell mysql to treat the datetime string as a date:
$Today = date("Y-m-d h:i:s");
if ($result = $mysqli->query("SELECT * FROM `table` WHERE (DATEDIFF(DATE(`ExpireDate`), DATE('$Today')) < 30 AND `ExpireDate` >= '$Today') ORDER BY `StartDate` ASC")){
while($row = mysqli_fetch_array($result)){
//show records
}
mysqli_close($mysqli);
}
I am trying to select the date in the field end_date that is less or equal to current date but did not work and my field end_date as the same date format(08-09-2014) data as current date below is my code thanks
$currentdate = date("d-m-Y");
$query1 = "SELECT * FROM location WHERE end_date <= '$currentdate'";
$result1 = mysql_query ($query1) or die('query error');
while( $line1 = mysql_fetch_assoc($result1)){
echo $line1['end_date'];
}
try this
SELECT * FROM location WHERE end_date <= DATE_FORMAT(CURDATE(), '%Y-%m-%d')
try with -
SELECT * FROM location WHERE end_date <= DATE_FORMAT(CURDATE(), '%d-%m-%Y')
$currentdate = date("Y-m-d");
$query1 = "SELECT * FROM location WHERE `end_date` <= '$currentdate'";
$result1 = mysql_query ($query1) or die('query error');
while( $line1 = mysql_fetch_assoc($result1)){
echo $line1['end_date'];
}
try this... must have in database date format;;
If your currentdate is independent of the system date (e.g. if you are operating over different timezones), and if, for some reason, your end_date is not a date type, then try this:
SELECT * FROM location
WHERE str_to_date(end_date, '%d-%m-%Y') <= str_to_date('$currentdate', '%d-%m-%Y')
where the format can be changed to match your inputs.
If you want to compare dates, then make sure you are comparing dates and not strings.
Using MySQLi and PHP I have a Query Like below and I need to store the orderdate field from MySQL newOrder table (with format of Datetime like 2014-06-15 22:12:00) into a PHP variable like $orderTime and then add 1 Hour to the Time.
<?PHP
include 'conconfig.php';
$con = new mysqli(DB_HOST,DB_USER,DB_PASS,DB_NAME);
$query = "select * from newOrder WHERE orderdate < NOW() ORDER BY id DESC LIMIT 1";
if ($result = $con->query($query))
{
if ($result->num_rows > 0)
{}
else
{
$resultStr = 'Nothing found';
}}
}
$con->close();
is it possible something like inside the while loop?
$orderTime = $row['orderdate'];
$orderTime = $orderTime +1hour;
Thanks
Easily done with date() and strtotime():
$orderTime = date('Y-m-d H:i:s', strtotime($row['orderdate'] . '+ 1 hour'));
Update to answer question in comment:
To check if difference between $ordertime and current time is greater than 2 hours:
if (time() - strtotime($orderTime) > 7200) {
echo 'Difference is greater than 2 hours';
}
Im trying to do a mysql check if a record from $uid exist from today based on $timestamp and if it doesnt then do an INSERT.
//EXAMPLE RECORD FROM TABLE VOTE
--- #vote_fb_uid# --- #vote_time#
665414807 1369219044
tjt
//STEP 1 - do a look up on $uid and check with timestamp $today
$timestamp = $this->time;
$date = date('Y-m-d', $timestamp);
$today = date('Y-m-d');
$sql = "
SELECT * FROM vote WHERE
vote_fb_uid = '$this->fb_uid',
WHERE vote_time = '$CHECK_IF_THERE_IS_AN_ENTRY_FROM_TODAY'";
$res = mysql_query($sql) or die( mysql_error());
//STEP 2 - If no records are found for today - then we do an INSERT
if($no_record_for_today) {
$sql = sprintf("
INSERT INTO vote(
vote_fb_uid,
vote_time)
VALUES ('%s','%s')",
mysql_real_escape_string($this->fb_uid),
mysql_real_escape_string($this->time));
$res = mysql_query($sql) or die( mysql_error());
}
Obviously im strugling with the SQL part for the look up - im wondering if there isnt some in-built SQL function to do this or similar?
to check if you had a vote in the last 24 hours :
SELECT *
FROM vote
WHERE vote_fb_uid = '$this->fb_uid'
AND FROM_UNIXTIME(vote_time) >= DATE_SUB(NOW(), INTERVAL 1 DAY)
if you want to limit to the same day (mean you are allowed to post at 2013.05.21 23:55 and 2013.05.22 00:05)
SELECT *
FROM vote
WHERE vote_fb_uid = '$this->fb_uid'
AND DATE(FROM_UNIXTIME(vote_time)) = DATE(NOW())
CURDATE()
Returns the current date as a value in 'YYYY-MM-DD' or YYYYMMDD format, depending on whether the function is used in a string or numeric context.
mysql> SELECT CURDATE();
-> '2008-06-13'
mysql> SELECT CURDATE() + 0;
-> 20080613
Try this:
$today = date('Y-m-d'); //change it to timestamp if you want in timestamp
$sql = "
SELECT count(*) as total FROM vote WHERE
vote_fb_uid = '$this->fb_uid' and
vote_time = '$today'";
$res = mysql_query($sql) or die( mysql_error());
if($res[0]['total'] < 1){
$sql = sprintf("
INSERT INTO vote(
vote_fb_uid,
vote_time)
VALUES ('%s','%s')",
mysql_real_escape_string($this->fb_uid),
mysql_real_escape_string($this->time));
$res = mysql_query($sql) or die( mysql_error());
} else{
//return error("custom","","Already Inserted.");
echo "already inserted";
}
Your $sql query have a syntax error, you have used two times clause WHERE the correct syntax to use two or more clauses in where is using AND to join them, to get only record wich don't have an entry for today you can use DATE_SUB with 1 day interval
SELECT *
FROM vote
WHERE vote_fb_uid = '$this->fb_uid',
AND vote_time <= DATE_SUB(vote_time, INTERVAL 1 DAY)
I was wondering what is the best way to write the where statement in PHP where targetDate < Date.Now - HardCodedHours in PHP
If you mean how to do it in an MySQL query:
SELECT * FROM table WHERE targetDate <= date_sub(now(), interval 1 hour);
This will pull "field1" from table "myTable" where a DATETIME column "targetDate" is older than 12 hours.
$hardcodedHours = 12;
$sql = "SELECT field1 FROM myTable WHERE targetDate <= '" . date('Y-m-d H:i:s', strtotime("-$hardcodedHours hours")) . "'";
$result = mysql_query($sql);
$limitTime = time() - $nbHours * 3600;
$query = "SELECT ... WHERE TIMESTAMP(targetDate) < $limitTime;";
Or something like that.