$today = date('Y-m-d H:i:s'); // get date SQL format
$today = date('Y-m-d H:i:s', strtotime($today)); // get date SQL format¨
$today = new DateTime($today);
$compare = new DateTime($row['date']);
$interval = date_diff($today, $compare);
echo $interval->format("%H") .'<br>';
echo $interval->format("%i") .'<br>';
echo $interval->format("%s") .'<br>';
die();
Something is very wrong, i want the time difference between a post in my SQL database and NowTime
$row['date']
is 2013-03-25 14:22:53 inserted as datetime in my table, for some reason i get a odd downcounting output?
Use the database query to do it.
SELECT DATEDIFF(date,NOW()) as DateDifference FROM table
Unsure why people keep insiting on doing db date comparisons in php rather than the DB. Its considerably quicker and less resource and tidier code to do it on query then just compare the result as it spits out the day differential of the dates. (date may be a protected column name so you may need to enclose it in ` or change it to a different column name.
Related
Ive checked the leads here and have not found the right solution,
<?php
$mysqldate = $row['callbackdate'];
$phpcurrentdate = strtotime( $mysqldate );
if ( $phpcurrentdate == date("Y-m-d") ) {
echo $row['last_name'];
}else{
echo "Nothing";
}
?>
The date field in sql is date and the format is YYYY-mm-dd. The answer always being returned is "Nothing". I know this sort of question has many variations but Ive had no luck finding this type. Im not looking for a range sort just a simple match is all... and the data $row['callbackdate']is functioning as Ive tested all other connections.
So Id appreciate any help! Thanks,
Les
$mysqldate = "2017-08-28"; //example
$phpcurrentdate = DateTime::createFromFormat('Y-m-d', $mysqldate); //put your format
$today= new DateTime("now");
if ( $phpcurrentdate == $today ) {
echo 'last_name';
}else{
echo "Nothing";
}
The dates are stored as string in your database. As possible solution to this is formatting both as dateTime for example:
$today = date('Y-m-d H:i:s');
$databaseDate = '2017-08-06 20:50:38';
var_dump(new DateTime($today) > new DateTime($databaseDate)) // returns true
var_dump(new DateTime($today) < new DateTime($databaseDate)) // returns false
Another way to do this is to make your columns type in the database a DateTime. This way you only have to format the current date as a DateTime and then you can compare them
I want to compare two dates and time values in PHP. One date is coming from MySQL, and second one is the current date. I want to run some code when both dates are the same. I tried the code below, but condition satisfies any time which is wrong.
$current_datetime = date('Y-m-d H:i');
$send_date = date("Y-m-d H:i", strtotime($row['send_date'])); // suppose $row['send_date']'s value is '2016-10-17 15:00'
if($current_datetime == $send_date){
//I want to run some code here
}else{
}
What is wrong with the code? I also tried to covert both dates with strtotime() before comparing, but it gave me the same issue. The above condition satisfies any time even if both dates are different.
Try this :
$current_datetime = date('Y-m-d H:i');
$send_date = date("Y-m-d H:i", strtotime($row['send_date'])); // suppose $row['send_date']'s value is '2016-10-17 15:00'
if(strtotime($current_datetime) == strtotime($send_date)){
//I want to run some code here
}else{
}
Hope it helps !!!!
One way is to fetch the Unix timestamp (seconds since '1970-01-01 00:00:00' UTC) from MySQL, then operate on the numbers:
$row = get_db_row("SELECT UNIX_TIMESTAMP(send_date) AS send_date_ts
FROM table WHERE $condition");
$hours = (int) ($row['send_date_ts'] / 3600);
$current_hours = (int) (time() / 3600);
if ($hours == $current_hours) {
// current hour
}
Timestamps are convenient because:
there is no need to take the format into account;
operations on numbers are usually faster;
the code looks cleaner.
Try this. On my server is working just great I've got something else because they aren't equal. Date which I receive from database is type datetime format 2015-04-13 09:03:49
<?php
$current_datetime = strtotime(date('Y-m-d H:i'));
$send_date = date("Y-m-d H:i", strtotime($row['send_date'])); // suppose $row['send_date']'s value is '2016-10-17 15:00'
if($current_datetime == $send_date){
//I want to run some code here
echo 'something';
}else{
echo 'something else';
}
Output:
echo $current_datetime . '<br/>';
2016-10-17 09:19
echo $send_date .'<br/>';
2015-04-13 09:03
// result
something else
I've created an If an Else Statement but it doesn't work properly.
I have some dates within my SQL which have been retrieved and stored in variables using PHP.
I'm comparing the current date with the dates from the database but for some reason, it thinks for example that 29-09-2015 if LESS THAN 31-01-2015.
I can understand that the format could be the issue d,m,Y but I thought I'd corrected that already.
Here's the code:
$today = date('d-m-Y');
$date = $row['respondby'];
$euDate= date("d-m-Y", strtotime($date));
<?php
if($today < $euDate){ echo "<td>". $today." is less than ". $euDate ."</td>";
}
else{
echo"<td>Lost somewhere in between ?!?!?! :S </td>";
}
?>
As a result it prints
29-09-2015 is less than 30-06-2015
today's date was 29-09-2015 and one of the dates was in the data was this one as shown.
Thank you everyone that helps.
Comparison of dates as strings uses lexicographical order, hence your result is "correct".
Instead of d-m-Y format, try to use Y-m-d, this guarantees proper ordering.
$today = date('Y-m-d');
$date = $row['respondby'];
$euDate= date("Y-m-d", strtotime($date));
if($today < $euDate) { [...] }
Or, you can use Date objects instead:
$today = new Date('now');
$euDate= new Date($row['respondby']);
if($today < $euDate) { [...] }
I want to insert a DateTime object in database where the column type is DateTime. How can I achieve this?
I am using this code:
$cdate = new DateTime('now')
$cd = $cdate->format('d/m/Y h:i:sa')
$udate = new DateTime('72 hours');
$ud = $udate->format('d/m/Y h:i:sa')
$insert = "insert into `winpc_user(mac_address,reg_date,updated_date,status,processor_name,ram_size,os_Name, os_Bits) values('$mac','$cdate','$udate','$stat','$proName','$rSize','$osName','$osBits')"
Same as the comment above, DATETIME's format is:
YYYY-MM-DD HH:MM:SS
Quite straightforward to follow using date()'s format function, it'll share the same with the ->format():
->format('Y-m-d H:i:s');
Sidenote: Of course, this needs to be quoted as well on insertion.
As an alternative, you could also use MySQL functions to achieve the same goal:
NOW()
DATE_ADD(NOW(), INTERVAL 72 HOUR)
I have a date returned from an sql query (a datetime type field) and want to compare it to today's date in PHP. I have consulted php manual and there are many ways to do it. I finally came up with a solution comparing strings, but I would like to know if there are either any 'better' (best practice), cleaner or faster ways to do it. This is my solution:
// $sql_returned_date='2008-10-17 11:20:04'
$today = new DateTime("now");
$f_today=$today->format('Y-m-d'); //formated today = '2011-03-09'
$sql_date=substr($sql_returned_date,0,9); //I get substring '2008-10-17'
if($f_today==$sql_date)
{
echo "yes,it's today";
}else{
echo "no, it's not";
}
thanks
Seriously guys?
//$mysql_date_string= '2013-09-20' OR '2013-09-20 12:30:23', for example
$my_date = new DateTime($mysql_date_string);
if($my_date->format('Y-m-d') == date('Y-m-d')) {
//it's today, let's make ginger snaps
}
You could factor this into the data returned from your database query:
SELECT `DateOnDB`,
DATE(`DateOnDB`) = DATE(CURDATE()) AS isToday
FROM `dbTable`
and simply use PHP to test the value of the isToday column
Excuse me for being a question-digger, but I was trying to achieve the same thing, and I found a simple solution - if you want to select only rows with today's date you can do :
WHERE DATE(datetime_column)=CURDATE()
in your mySQL query syntax.
You'd have three solutions :
Working with strings, like you are doing ; which seems like a solution that works ; even if it doesn't feel clean.
Working with timestamps, using strtotime() and time() ; which is a bad idea : UNIX Timestamps only work for dates that are greater than 1970 and lower than 2038
Working with DateTime everywhere ; which would both work and feel clean.
If I need to make any calculation on the PHP-side, I would probably go with the third solution -- but the first one would be OK in most cases, I suppose.
As a sidenote : instead of formating your date to Y-m-d, you could check if it's :
Greater of equal than today
Less than tomorrow.
If SQL returned date is in this format 2011-03-09 (date format without timing),
$sqlret = "2011-03-05";
$curdate = date('Y-m-d');
echo $diff = strtotime($curdate) - strtotime($sqlret);
echo $no_diff = $diff/(60*60*24);
If the date with time like:
$sqlret = "2011-03-05 12:05:05",
Just make your current date format also like that:
$curdate = date('Y-m-d H:i:s');
If it doesn't satisfies your need, ask your question with some example.
You can use new DateTime php Object that way.
$date1 = new DateTime('2012-01-21');
$date2 = new DateTime ( 'now');
$interval = $date1->diff($date2);
if( $interval->format('%R%a ') == 0){
echo 'it s today';
}
I'd do that:
# SQL
SELECT DATE_FORMAT(date_col, "%Y-%m-%d") AS created_at FROM table
# PHP
if ( date('Y-m-d') == $sql_date ) { // assuming $sql_date is SQL's created_at
echo 'today';
}
$time = //your timestamp
$start = mktime(0,0,0,date("j"),date("n"),date("Y"));
$end = mktime(23,59,0,date("j"),date("n"),date("Y"));
if($time > $start && $time < $end){
//is today
}