How do you check time in a DB to todays date? - php

I'm trying to write a conditional that would check a date in a table. If the date in the table is the same as todays date, it would ignore. If not it wouldn't break.
if(strtotime($row['timefield']) == (date(Y-m-d)){
break 1;
}
^ This doesn't work but it's sort of what I am trying to do. Thoughts?

You can just do it on the database side if your database is using the local time/date.
(Assuming MySQL, and timefield is a date type):
SELECT
CASE
WHEN timefield = CURDATE()
THEN 1
ELSE 0
AS is_today
If timefield is datetime then:
SELECT
CASE
WHEN DATE(timefield) = CURDATE()
THEN 1
ELSE 0
AS is_today

Assuming the timefield is of DATE type:
if (strtotime($row['timefield']) != date('Y-m-d')){
// Do insert
}

I'm pretty sure the problem is in the formatting of your datetime. Instead compere something that is much for comparable and rigid, like unix timestamp.
So basically what NullPointer just said... Damn that was quick :D

Related

How to form this zend_db_select statement to compare timestamp

This is what i am trying to do.
id ip timestamp userid
9 127.0.0.1 2013-06-27 16:52:49 35
I would like to first query table to see if ip for the user is persisted. If so, he/she voted 2) Check timestamp of vote against current timestamp, if his voting time is way after 24 hours, then allow him to vote again. Ensure he can only vote once in 24 hours.
This is my current zend_db_select statement. Has some problems coming out with the logic
function checkIfCanLikeH($hlid,$ip){
$canlike = false;
$currenttime = time();
$checkquery = $this->dbo->select()
->from("hl", array("num"=>"COUNT(*)"))
->where("hlid = ?", $hlid)
->where("ip = ?", $ip)
->where("timestamp < ".new Zend_Db_Expr('NOW()'));
$this->dbo->setFetchMode(Zend_Db::FETCH_ASSOC);
$checkrequest = $this->dbo->fetchRow($checkquery);
if($checkrequest['num']!=0){
$ip = true;
}
return $ip;
}
I am stucked at how do i compare the timestamp. I need retrieve the timestamp from table and check if the current time is smaller than value in table (added 24 hours).
Advice appreciated.
->where("timestamp < DATE_SUB(NOW(), INTERVAL 1 DAY)");
This is the advantage of using MySQL's timestamp/datetime/date column types - you can use its inbuilt date and time functions. And I'm pretty sure you don't need Zend_Db_Expr here.
Edit: I'm not completely clear what you're asking, but I think the WHERE clause above does what you want, just the other way around. You could do this instead:
->where("DATE_ADD(timestamp, INTERVAL 1 DAY) < NOW()");
which is the same thing but written a different way. This means "where timestamp + 1 day is less than the current time".
If it helps, try running this query on your database:
SELECT DATE_ADD(timestamp, INTERVAL 1 DAY), NOW() FROM hl
so you can see how the DATE_ADD() function in MySQL works.

Datetime format check in php

How would i go about checking to see if an auction has expired in my database? I have a datetime column in MySQL that i believe is of the following format: YYY-MM-DD hh:mm:ss. If this is the case would the following check work - i.e. want to select only expired auctions from the table in the database...
<?php
//Some code
$auctioncheck = mysql_query("
SELECT * FROM auction WHERE ($date_time > finish_time)
");
?>
While "finish time" is a column in the database of the above cited format. Presuming this works how actually do i get the current date into the same format? If anybody knows i would very grateful cheers. Even more so if the above query wouldn't work and something else is required. Thanks again.
Oh and of course i would define the date_time variable to start with
Do you actually need the $date_time variable? The easiest way to do this would be SELECT * FROM auction WHERE finish_time < NOW(). That way you'll get your results and don't have to set the date from PHP.
You can use
<?php
//Some code
$date_time=strtotime($date_time);
$auctioncheck = mysql_query("
SELECT * FROM auction WHERE ( $date_time > UNIX_TIMESTAMP(finish_time))
");
?>
This solution:
// to show both date and time,
$date->get('YYYY-MM-dd HH:mm:ss');
// or, to show date only
$date->get('YYYY-MM-dd')
is from this post: PHP Zend date format
and here's another solution: how to format a Date in MM/dd/yyyy HH:mm:ss format in javascript?
and to do it in php:
How to get the current date and time in PHP?
The function date is made for it:
// Get the current date
$date_time = date("y-m-d H:i:s");
Look at the documentation page to see other format flags ;)

Query for events with a date and time greater than now

all - fairly simple query question that's been hounding me: How do I query for entries with a date and time only greater than now, or when the query is run (page requested)?
I've seen some examples but they aren't good enough for me to modify. Here's my code:
$todaysDate = date("Y-m-d h:i:s");
$params = array('select'=>'*', 'limit'=>3, 'orderby'=>'t.event_StartDate ASC', 't.event_StartDate < "$todaysDate"' );
An example of the variable "t.event_StartDate" outputs "2010-12-10 22:18:42" so I assume that may be how it's saved in the database.
I suspect I'm not building the date correctly or I need to be using a time function I'm not familiar with in MySQL. Help? This is for outputting a series of events with date and time.
I think you reverse the use <, it should be >
try
SELECT ... WHERE t.event_StartDate>NOW();
/* you don't even need to set $todaysDate */
t.event_StartDate > NOW() don't will returns nothing? a date higher of now lol. I'm confused.

query to find a partial match

I want to write a query to find all the items in my 'schedule_items' table that match a YYYY-MM-DD value of a DATETIME field.
something like: SELECT * FROMschedule_itemsWHEREstart?matches? '$date'
Where start would be a DATETIME field with data like 2009-09-23 11:34:00 and $date would be something like 2009-09-23
Can I do this or do I need to load all the values and then explode and compare the values in my DATETIME start field?
You can use LIKE in MySQL
SELECT * FROM schedule_items WHERE start LIKE 'some-date%'
You put a % sign as a wildcard.
Be aware though that like can be a very heavy query. Specially if you use % at the start of a string.
If you are working with dates, like it seems you are saying :
Where start would be a DATETIME field
with data like 2009-09-23 11:34:00 and
$date would be something like
2009-09-23
Using LIKE might not be the only/best solution : there are functions that deal with dates ; and you probably can use comparison operators too.
In your case, you can probably use something like this :
select *
from headers_sites
where date_fetch >= '2009-07-15'
limit 0, 10
Of course, you'll have to adapt this query to your tables/fields ; something like this might do, I suppose :
SELECT * FROM schedule_items WHERE start >= '$date'
This will get you every data for which the date is more recent than $date.
If you only want the date of one day, this could do :
SELECT *
FROM schedule_items
WHERE start >= '$date'
and start < adddate('$date', interval 1 day)
This might be better than "like", if you start column has an index -- not sure, though ; but, still, what you are willing to get will be obvious from your query... And that is nice.
in SQL you can use LIKE instead of = and then you get the % wildcard. Example:
select ... where start like "2009-09-%";
(That would match anything from August 2009)
If it is needed to extract records of a date from datetime field, you need to use following format:
SELECT * FROM schedule_items WHERE date(start) = 'some-date time'

How to use the mysql "LIKE" syntax with a timestamp?

I want to allow my users to search the database for a row that was submitted on a certain day. The date is entered into the field in the database using the date() function which is great, but when i use the php strtotime function, of course the dates are not exactly the same.
Is there some clever mysql function that can help me with this?
I had an issue with this before.
You're best to generate a start and end date then use the BETWEEN function instead.
So something like this:
$start_date = "2009-01-01 00:00:00";
$end_date = "2009-01-01 23:59:59";
$sql = "SELECT * FROM table WHERE date BETWEEN '$start_date' AND '$end_date' AND id = 'x';
Of course you would just pull your dates from the DB using strtotime and append the time stamps - depends how you used date()
Hope this helps :)
You can use MySQL's DATE() function to extract date part of the timestamp:
select ... from table ... where DATE(date_column) = '2010-01-25';
If you have problem submitting '2010-01-25' from PHP, you can use PHP's date function with 'Y-m-d' as parameter to only get the date part.
$d = date('Y-m-d', strtotime(...));
Looking at your question closely, it seems you'll need both of those. First use PHP's date function to get only the date part and then use MySQL's date to match only those records.
PHP:
$timestamp_from_php = strtotime('December 25, 2009');
SQL:
select
`fields`
from
Table t
where
date_format('Y-m-d', t.`datetime_field`) = date_format('Y-m-d', '$timestamp_from_php')

Categories