I'm trying to hide an event two days after it's passed in php (using a mysql query). There are two date options. Start date, and End date. I can't seem to figure out how to make the query work.
$query->select('*');
$query->from('#__events_items');
$query->where('date2 >= "'.$today.'"');
$query->where('date2 <= "'.$sixmths.'"');
$query->where('state = 1');
$query->order('date1 asc');
I've tried
$today = #date('Y-m-d');
$enddate = #date('Y-m-d',(strtotime(#date('Y-m-d')."+ 2 days")));
But obviously the end date won't be greater or equal to $enddate.. Any help would be greatly appreciated!
You can either modify today like so:
$today = date('Y-m-d', strtotime('-2 days'));
Or modify your query like so :
$query->select('*');
$query->from('#__events_items');
$query->where('date2 >= "'.$today.'" - INTERVAL 2 DAY');
$query->where('date2 <= "'.$sixmths.'"');
$query->where('state = 1');
$query->order('date1 asc');
I would say the second is probably better form since having $today represent two days ago would be nasty self documenting code.
Related
I am trying to compare to date to figure out how much time is between them, which I know how to do, date_diff(), but I want to then compare the time between the dates and if it is greater than 7 days do something and if not do something else. I think it sounds easy and I know there are probably fairly simple solutions to do so but I am just not a fan of dates and comparisons. Here is a snippet of what I got so far as it is just one case of a switch statement so the rest are basically identical.
$array = array();
$today = date("Y-m-d"); // get today's date
foreach($arrayOfObjs as $obj){
if ($obj->get("renewalDate") >= $today){
array_push($array, $obj->get("renewalDate"));
}else{
switch($obj->get("recurrencePeriod")){
case 1:
/*
* All cases follow same structure
* Build the date in format Y-m-d from renewalDate out of the obj.
* Loop through the date while it's less than today.
* After date is greater than today return date add to array
*/
$date = DateTime::createFromFormat("Y-m-d", $obj->get('renewalDate'));
while($date <= $today){
$date->add(new DateInterval('P7D'));
}
$diff = date_diff($today, $date);
if($diff->format('%a') <= 7){
$obj->renewalDate($date);
array_push($array, $obj);
}
break;
Basically, my database stores dates and those dates could be passed but it could be a reoccurring event. To calculate the next time that event would happen I check if the data in the database is before today's date and if it is then I continue to add the incremental amount (in this case 7 for a weekly reoccurring event) and compare the dates again. After the date that is incremented passes today's date I want to find out if it is within 7 days and if so add it to an array to get returned. I know... since I'm adding 7and it's within 7 days the first reoccurring event will always be within 7 days but that is not the case for monthly events or anything greater.
All cases are broken so I only included this one for simplicity. I can get date_Diff to return something like 7 or 12 or whatever the number may be but how can I check if that number is within the 7 days I want?
Thanks, I will include more information if needed to clarify any misunderstandings.
I'm not entirely sure what you are trying to do, but how about the following if you are just projecting dates forward and backwards and want to know if they are 7 days or more either way:
$today = date("Y-m-d");
$todaytime = strtotime($today);
$testdate = "2017-06-31";
$testtime = strtotime($testdate);
$back7days = strtotime("-7 days",$todaytime);
if($testtime < $back7days)
echo "X"; // do somthing if testdate was more than 7 days ago
$fwd7days = strtotime("+7 days", $todaytime);
if($testtime > $fwd7days)
echo "Y"; // do somthing if testdate is more than 7 days in future
Just make sure that you use less-than or less-than-and-equals comparators etc to handle the boundary conditions you need.
I'm trying to take a date and add 3 months to it for use on an accounting system and we need to split the dates into quarters for our tax return
I have the following code
$e = ORM::for_table('sys_taxdate')->find_many();
This has been input into the database using a date type in the column and displys correctly as :
2016-07-02
I then want to add 3 months to this date so i can search the database for any invoices rasied between these 2 dates. The code i have tried is
$idate = $e;
$its = strtotime($idate);
$dd = date('Y-m-d', strtotime('+3 months', $its));
But $dd outputs the date 1970-04-01
I have looked over many posts and looks as though strtotime is not starting with the right date format, or at least thats what i think but have been trying for hours now and have hit a brick wall
I figured it out.
I ended up getting the exact result from the database first
$e = ORM::for_table('sys_taxdate')->find_one(2);
$date = $e->get('taxdate');
$idate = $date;
$its = strtotime($idate);
$dd = date('Y-m-d', strtotime('+3 months', $its));
How exactly is this done? There's so many questions on stack-overflow about what I'm trying to do; However all of the solutions are to edit the MYSQL Query, and I need to do this from within PHP.
I read about the strtotime('-30 days') method on another question and tried it, but I can't get any results. Here's what I'm trying:
$current_date = date_create();
$current_date->format('U');
... mysql code ...
$transaction_date = date_create($affiliate['Date']);
$transaction_date->format('U');
if($transaction_date > ($current_date - strtotime('-30 days'))) {
} else if(($transaction_date < (($current_date) - (strtotime('-30 days'))))
&& ($transaction_date > (($current_date) - (strtotime('-60 days'))))) {
}
Effectively, I'm trying to sort all of the data in the database based on a date, and if the database entry was posted within the last 30 days, I want to perform a function, then I want to see if the database entry is older than 30 days, but not older than 60 days, and perform more actions.
This epoch math is really weird, you'd think that getting the epoch of the current time, the epoch of the data entry, and the epoch of 30 and 60 days ago would be good enough to do what I wanted, but for some reason it's not working, everything is returning as being less than 30 days old, even if I set the date in the database to last year.
No need to convert to unix timestamp, you can already compare DateTime objects:
$current_date = data_create();
$before_30_day_date = date_create('-30 day');
$before_60_day_date = date_create('-60 day');
$transaction_date = date_create($affiliate['Date']);
if ($transaction_date > $before_30_day_date) {
# transation date is between -30 day and future
} elseif ($transaction_date < $before_30_day_date && $transaction_date > $before_60_day_date) {
# transation date is between -60 day and -30 day
}
This creates (inefficiently, see my comment above) an object:
$current_date = date_create(date("Y-m-d H:i:s"));
From which you try to subtract an integer:
if($transaction_date > ($current_date - strtotime('-30 days'))) {
which is basically
if (object > (object - integer))
which makes no sense.
you're mixing the oldschool time() system, which deals purely with unix timestamps, and the newer DateTime object system, which deals with objects.
What you should have is
$current_date = date_create(); // "now"
$d30 = new DateInterval('P30D'); // 30 days interval
$transaction_date = date_create($affiliate['Date']);
if ($transaction_date > ($current_date->sub($d30)) { ... }
You might consider DatePeriod class, which in essence gives you the ability to deal with a seires of DateTime objects at specified intervals.
$current_date = new DateTime();
$negative_thirty_days = new DateInterval::createFromDateString('-30 day');
$date_periods = new DatePeriod($current_date, $negative_thrity_days, 3);
$thirty_days_ago = $date_periods[1];
$sixty_day_ago = $date_periods[2];
Here you would use $thirty_days_ago, $sixty_days_ago, etc. for your comparisons.
Just showing this as alternative to other options (which will work) as this is more scalable if you need to work with a larger number of interval periods.
I have a database with different workdates, and I have to make a calculation that generates more dates based on a weekinterval (stored in the database) and the (in the database stored) days on which the workdays occur.
What my code does now is the following:
Read the first two workdates -> Calculate the weeks inbetween and save the week interval
Read all the workdates -> fill in the days on which a workdate occurs and save it in a contract.
Generate workdates for the next year, based on the week interval.
The point is: for each week with a week interval of 1, more days of the week should be saved as a workdate. I've used this code to do this, but it doesn't work.
// Get the last workdate's actdate.
$workdate_date = $linked_workdate['Workdate']['workdate_actdate'];
// Calculate the new workdate's date
$date = date("Y-m-d", strtotime($workdate_date . "+" . $interval . " week"));
// If 'Monday' is filled in for this contract, calculate on which day the
// Monday after the last interval is. Same for each day, obviously.
// The days are boolean.
if ($contract['Contract']['contract_maandag'] = 1){
$date = date("Y-m-d", strtotime($date, "next Monday"));
}
if ($contract['Contract']['contract_dinsdag'] = 1){
$date = date("Y-m-d", strtotime($date, "next Tuesday"));
}
// After this, save $date in the database, but that works.
Here is the error that i get:
strtotime() expects parameter 2 to be long, string given
I'm quite stuck right now, so help is appreciated!
if ($contract['Contract']['contract_maandag'] = 1){
if ($contract['Contract']['contract_dinsdag'] = 1){
This won't work. You're doing an assignment (=), so it's always true. But you want a comparison (===). It is recommended to do always (except required otherwise) to use strict (===) comparison.
Well, the = doesn't seem to be the problem, since the error is about the part that's after the comparison. Try
strtotime("$date next Monday");
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"));