Say, I have 4 dates and the date today is 1/7/16:
1/7/16 1:06:02
1/7/16 8:01:24
1/8/16 7:02:23
1/6/16 3:12:34
How can I only pick 1/7/16 1:06:02 and 1/7/16 8:01:24.
What date function from PHP can I use to only get the date of today?
Thanks!
UPDATE:
This is being used as a MYSQL selector.
Example: $db->query("DELETE * FROM entry WHERE date='$today'");
How would this work? How can I get my MYSQL query to select only the dates for today?
UPDATE 2:
I've tried using curdate(), but the code is not working...
This is what I'm doing:
$db->query("DELETE * FROM entry WHERE date=curdate()");
What am I doing wrong?
LAST UPDATE: curdate() worked properly...
UPDATE: As MySQL query, you can do this as follows:
$db->query("DELETE * FROM entry WHERE DATE(date) = CURDATE()");
CURDATE() returns today's date.
If you want to do with php, see below to use the DateTime class.
$now = new DateTime;
$otherDate = new DateTime('2016-01-01'); // or e.g. 2016-01-01 21:00:02
// Setting the time to 0 will ensure the difference is measured only in days
$now->setTime( 0, 0, 0 );
$otherDate->setTime( 0, 0, 0 );
var_dump($now->diff($otherDate)->days === 0); // Today
var_dump($now->diff($otherDate)->days === -1); // Yesterday
var_dump($now->diff($otherDate)->days === 1); // Tomorrow
The answer by schellingerht is totally correct, but
$now = new DateTime;
should be this instead:
$now = new DateTime('Today');
That will ensure that the date from today with 0 hours and 0 minutes is selected. Otherwise the output could be Today even if the date is tomorrow.
Hope that helps!
Convert to timestamp and then format for only the date. Then compare to the same format of todays date:
var_dump(
date('Ymd', strtotime('1/7/16 1:06:02')) === date('Ymd')
);
Try using DATE() (http://www.w3schools.com/sql/func_date.asp). You don't need to convert your dates to a string - this is not efficient.
DELETE FROM yourTable WHERE DATE(yourDateField) = DATE(NOW)
You could compare today's date with Y-m-d format, with your date parsed with the same format.
$today = date('Y-m-d');
$otherdate = date('Y-m-d', strtotime('2022-02-02 16:00:26'));
if($today === $otherdate){
\\ your code for Today
} else {
\\ your code for not Today
}
Related
I'm trying to do a system that compare an specific date ($date) with the atual date (date('Y/m/d')), but I need to color this $date when he is one week to the atual date (near the maturity date). I don't know if you can understand me, my english is bad...
Thank you for reading this.
Do it in the MySQL query:
SELECT maturity_date,
maturity_date < DATE_ADD(NOW(), INTERVAL 1 WEEK) AS near_maturity,
...
Then your PHP code can use if ($row['near_maturity') to color the date.
Something like this may work:
date_default_timezone_set('America/Los_Angeles');
$date = DateTime::createFromFormat('m-d-Y', '04-15-2013');
$maturityDate = DateTime::createFromFormat('m-d-Y', '04-20-2013');
$maturityDateMinus10Days = DateTime::createFromFormat('m-d-Y', '04-10-2013');
if ($date > $maturityDateMinus10Days
&& $date < $maturityDate) {
echo 'date is within 10 days of maturity 10';
}
You can test it by copying and pasting here http://writecodeonline.com/php/
Ref. PHP - add 1 day to date format mm-dd-yyyy
Ref. How can I check if the current date/time is past a set date/time?
i am struggling for a long time to set a specific date but i am not getting correct out put.
i want to get date from user and compare that date with the date 15 days older then today. if it is older than 15 days then convert to today else print what it is.
$todaydate= $_GET['date'];// getting date as 201013 ddmmyy submitted by user
$todaydate=preg_replace("/[^0-9,.]/", "", $todaydate);
$today =date("dmy"); //today ddmmyy
$older= date("dmy",strtotime("-15 day")); // before 15 days 051013
if ($todaydate <= $older){
$todaydate= $today;}
problem is, it is taking date as number and giving wrong result.
Comparing date strings is a bit hacky and prone to failure. Try comparing actual date objects
$userDate = DateTime::createFromFormat('dmy', $_GET['date']);
if ($userDate === false) {
throw new InvalidArgumentException('Invalid date string');
}
$cmp = new DateTime('15 days ago');
if ($userDate <= $cmp) {
$userDate = new DateTime();
}
Also, strtotime has some severe limitations (see http://php.net/manual/function.strtotime.php#refsect1-function.strtotime-notesand) and is not useful in non-US locales. The DateTime class is much more flexible and up-to-date.
try this one:
<?php
$todaydate = date(d-m-Y,strtotime($_GET['date']));
$today = date("d-m-Y");
$older= date("d-m-Y",strtotime("-15 day"));
if (strtotime($todaydate) <= strtotime($older))
{
$todaydate= $today;
}
?>
$previousDate = "2012-09-30";
if (strtotime($previousDate) <= strtotime("-15 days")) {
//the date in $previousDate is earlier or is equal to the date 15 days before from today
}
I am getting a date back from a mysql query in the format YYYY-MM-DD.
I need to determine if that is more than three months in the past from the current month.
I currently have this code:
$passwordResetDate = $row['passwordReset'];
$today = date('Y-m-d');
$splitCurrentDate = explode('-',$today);
$currentMonth = $splitCurrentDate[1];
$splitResetDate = explode('-', $passwordResetDate);
$resetMonth = $splitResetDate[1];
$diferenceInMonths = $splitCurrentDate[1] - $splitResetDate[1];
if ($diferenceInMonths > 3) {
$log->lwrite('Need to reset password');
}
The problem with this is that, if the current month is in January, for instance, giving a month value of 01, and $resetMonth is November, giving a month value of 11, then $differenceInMonths will be -10, which won't pass the if() statement.
How do I fix this to allow for months in the previous year(s)?
Or is there a better way to do this entire routine?
Use strtotime(), like so:
$today = time(); //todays date
$twoMonthsLater = strtotime("+3 months", $today); //3 months later
Now, you can easily compare them and determine.
I’d use PHP’s built-in DateTime and DateInterval classes for this.
<?php
// create a DateTime representation of your start date
// where $date is date in database
$resetDate = new DateTime($date);
// create a DateIntveral representation of 3 months
$passwordExpiry = new DateInterval('3M');
// add DateInterval to DateTime
$resetDate->add($passwordExpiry);
// compare $resetDate to today’s date
$difference = $resetDate->diff(new DateTime());
if ($difference->m > 3) {
// date is more than three months apart
}
I would do the date comparison in your SQL expression.
Otherwise, PHP has a host of functions that allow easy manipulation of date strings:
PHP: Date/Time Functions - Manual
I have a table with a date field type date.
What I am trying to do is to do a comparison between the date from inside the table and the today date. If the date from the table is yesterday then insert the today date.
The thing I'm not sure about is how to insert the data in the database so I can make the comparison. here is what im thinking to do"
$d = time();
$x = mysql_querry("SELECT date FROM table where id = $id", $con);
while($y = myqsl_fetch_array($x)){
$oldTime = $y['date'];
}
if ($oldTime < $d){
$i = mysql_querry("INSERT INTO table (date) VALUES (CURDATE()) ", $con);
}
So, I'm not sure if $oldTime and $d can be compared like that, but I hope you guys get my point.
Any ideas?
Thanks
You can't do in that way because the CURDATE() function return a date in a format like 2011-11-11 while time() returns the number of seconds since the January 1 1970 00:00:00 GMT.
Anyway you can change the format of the time() to look like the CURDATE() using the date() function in this way:
date('Y-m-d', time())
or even better, to get the current date, you can use just this line:
date('Y-m-d')
To conclude, you can do the if in this way:
if( strtotime($oldTime) < strtotime(date('Y-m-d')) )
or even better:
if( strtotime($oldTime) < strtotime('now') )
To compare dates you can use strtotime($date); Where date can be a time(), mysql date or date('Y-m-d'); string
I was looking at this post, and it is close to what I need:
PHP - How to count 60 days from the add date
However, in that post, the calculation is performed by adding 60 days to the current date. What I need to do is calculate the date based on a variable date (and not the current date).
Something like this:
$my_date = $some_row_from_a_database;
$date_plus_10_days = ???;
Anyone know how to do that?
Thanks
You can put something before the "+10 days" part:
strtotime("2010-01-01 +10 days");
Use date_add
http://www.php.net/manual/en/datetime.add.php
$my_date = new DateTime($some_row_from_a_database);
$date_plus_10_days = date_add($my_date, new DateInterval('P10D'));
You will have to look into strtotime(). I'd imagine your final code would look something like this:
$dateVariable = strtotime('2017-01-29');//your date variable goes here
$date_plus_60_days = date('Y-m-d', strtotime('+ 60 days', $dateVariable));
echo $date_plus_60_days;
If you are using PHP >= 5.2 I strongly suggest you use the new DateTime object. For example like below:
$date_plus_60_days = new DateTime("2006-12-12");
$date_plus_60_days->modify("+60 days");
echo $date_plus_60_days->format("Y-m-d");
I see you are retriving data from a database.
If you are using mysql you can do it on the select:
Example: you need the last date of the table and this date-7 days
select max(datefield) as ultimaf, DATE_SUB(max(datefield),INTERVAL 7 DAY) as last7
from table
It´s easy use curdate() if you want todays date.
If you need a dynamic between that selects the count of last 7 days:
select count(*) from table
where DATE_SUB(CURDATE(),INTERVAL 7 DAY)<=datefield"
date('Y-m-d H:i:s', strtotime("2014-11-24 06:33:39" +35 days"))
this will get the calculated date in defined format.
Suppose today's date is
date_default_timezone_set('Asia/Calcutta');
$today=date("Y-m-d");
And i can add 10 days in current date as follows :
$date_afte_10_days = date('Y-m-d', strtotime("$today +10 days"));