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"));
Related
So I have a database entry that update the date/time in yyyy-mm-dd hh:mm:ss.
Now I want to check, if there is a space inbetween the the database and the actual time from 60 minutes. How can I do that?
Example:
DB: 2020-02-14 10:00:00
Time now: 2020-02-14 11:01:00
Do something
DB: 2020-02-14 10:00:00
Time now: 2020-02-14 10:59:00
Do nothing
You can use something like this:
$t1 = strtotime( '2006-04-14 11:30:00' );
$t2 = strtotime( '2006-04-12 12:30:00' );
$diff = round(($t1 - $t2) / 3600);
In MySQL, you can do date arithmetics:
update mytable
set mydatetime = now()
where mydatetime <= now() - interval 1 hour and id = ?
The where clause filters on record whose column mydatetimeis more than one hour old. You did not tell what you want to do , so I assumed an update query, that resets the date/time to the current date/time.
This assumes that you want to update the timestamp of a given record, not accross the whole table, hence condition id = ?, which you can adapt - or remove - for your use case.
Hello Schmaniel at first i think you should use Carbon()
https://carbon.nesbot.com/docs/ to get the right results. It's a great way to work with timeformats and timestamps.
$now = Carbon::now();
$dbtime = Carbon::createFromFormat('Y-m-d H:i:s', '1975-05-21 22:10:22');
$totalDuration = $now->diffForHumans($dbtime);
dd($totalDuration);
$currentDate = new DateTime();
$databaseDate = new DateTime($dateFromDatabase);
$interval = $datetime1->diff($datetime2);
Then you can check using the $interval variable
You can use mysql TIMESTAMPDIFF like this:
SELECT TIMESTAMPDIFF(HOUR,'2003-02-01','2003-05-01 12:05:55');
you can use one of the follwing units:
MICROSECOND (microseconds), SECOND, MINUTE, HOUR, DAY, WEEK, MONTH,
QUARTER, or YEAR.
i have a problem with query SQL.
I need do a query that it result my order to last week ( 7days to now) I use this code
$this->db->where("account_orders.data > now()-7",NULL);
date in my db is -> 2019-04-05
Why don't it work ?
I dont know why are using this now()-7, you can use range here with where clause if you want orders between 7 days to now, as:
$dateLastWeek = date('Y-m-d', strtotime('-7 days')); // last week day
$dateCurrent = date('Y-m-d'); // current day
In CI, you can use range as like:
$this->db->where("account_orders.data >=", $dateLastWeek);
$this->db->where("account_orders.data <=", $dateCurrent);
If you just need last week orders then, you can use just $dateLastWeek date in your query as:
$this->db->where("account_orders.data", $dateLastWeek);
Note that, i am using Y-m-d date format due your input which you mentioned in your question 2019-04-05
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.
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 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))