PHP and MySQL - Comparing dates near maturity - php

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?

Related

How to store month and year only and set day to 1 by default for all dates?

I have a date as 2010-12-24.
I need to store the date as 2010-12-01 in database table. This should be applicable for all dates.
When inserting another date 2010-12-25, it should also be stored as 2010-12-01, and for 2017-01-29, it be 2017-01-01.
How can I set the day to default 1?
I hope this is clear. Any help is appreciated. Thanks.
A much simpler way is to use PHP's built-in date functions :
// here's your date
$date='2017-01-12';
// and here we transform it as we want
$date_first = date('Y-m-01', strtotime($date));
// check out the result :)
echo $date_first;
it can be done in mysql just fine, though.
set #date = '2017-12-25';
select str_to_date(CONCAT(YEAR(#date),' ',MONTH(#date),' 1'), '%Y %m %d');
You don't need to use the #date variable, just replace #date with the date you are need to insert and use str_to_date to replace the day with 1.
You can achieve it directly in MySql as follows:
SELECT DATE_SUB('2010-12-25', INTERVAL DAY('2010-12-25')-1 DAY)
Explanation:
1. DAY('2010-12-25')-1 DAY // will return 24
2. DATE_SUB('2010-12-25', INTERVAL 24) // will return 25-24 = 01 i.e. 2010-12-01
This will work in all cases.
This should work, adjust to your requirement
$today = date("Y-m", strtotime("2017-12-25"));
$dbDate = date ("Y-m-d", strtotime($today."-1"));
echo $dbDate;
Pass $dbDate to the query.
<?php
$date='2017-01-12';
$date=explode('-',$date);
$date[2]=str_replace($date[2],'01',$date[2]);
$date=implode('-',$date);
print_r($date);
And the output is :
2017-01-01
So it will always replace the day with 01.
After you can execute the insert query for the new date as it is already modified in the php.

How to check if date is today's date with PHP

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
}

how can i get which month a given year day is in in php?

I have been given year day (1-366) and I need to figure out which month it is in, how can I do this?
Well, I actually have a date string like : year, day or year, minute of day, second and I ultimately want to create a POSIX timestamp from it, how can I do this?
Thank you!
If you have PHP >= 5.3, then you can use DateTime::createFromFormat.
$day = 176;
$date = DateTime::createFromFormat('z', $day);
echo $date->getTimestamp(); // 1372275280
<?php
$year=2013;
$d=360;
echo date("m",strtotime("1/1/$year + $d days"))
?>
Use the date function to get a posix time stamp.
To get the month of a certain date, use intval(date('m'), mktime($h,$m,$s,$month,$day,$year))

Date comparison with PHP and Mysql

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

PHP Calculating future date by adding days to a variable date

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"));

Categories