I want to get all records from now between a year ago but it's not working exactly.
Made in CodeIgniter
This is my query , antything wrong with it ? :
$currentDate = date("Y-m-d H:m:s");
$yeardate=date('Y-m-d H:m:s', strtotime('-1 year'));
$this->db->select('TimeStamp');
$this->db->where('Transaction', 'COMPLETED');
$this->db->where('TimeStamp>=',$yeardate);
$this->db->where('TimeStamp<=',$currentDate);
$query = $this->db->get('R_Logs');
$results = $query->result();
Thanks in advance
I forgot to add a space between TimeStamp <= and TimeStamp >= .
My bad.
I think it might be easier to try Epoch time and then query off of that - e.g. Get epoch for a specific date using Javascript
That will convert the date to number of days since January 1, 1970. then from that you can subtract 365 days ( or use the yearInteger %4 trick to find out if it is a leap year)
Change these two lines any try once:
$currentDate = date("Y-m-d H:i:s");
$yeardate = strtotime(date('Y-m-d H:i:s') . ' -1 year');
Dump your query and debug:
echo $this->db->last_query();
Related
How do I fetch all of the rows that have a duedate -1 days from current day?
public function getNumTasksDueTomorrow()
{
$this->db->select('*');
$this->db->from('tasks');
$this->db->where("user_id",$this->session->userdata('user_id'));
$this->db->order_by("tasks_id", "desc");
$this->db->where("tasks_duedate", ' -1 day from current day');
$query_result=$this->db->get();
$result=$query_result->row();
return $result;
}
First get a mysql date for yesterday something like:
$date = date('Y-m-d H:i:s', strtotime('yesterday'));
Then change your date query to:
$this->db->where("tasks_duedate <", $date);
This will give you all tasks with a due date older than yesterdays date. However I think you mean any date in the past, so just change $date to now.
$date = date('Y-m-d H:i:s');
And of course I have assumed your date record is a mysql timestamp, you may have used a date, just remove the time formats.
Hope that helps,
Paul.
I can add x week to my date
//$ultima_azione <--- 2015/07/15
//$data['intervallo'] <---- 5
$mydate = date("Y-m-d",strtotime($ultima_azione." +".$data['intervallo']." weeks"));
now how can i give a day starting from that week
example:
//$mydate + "next Monday" -----> final date
and this ve to work like, if today is Monday and i add weeks to jump to an other Monday and then i select the next Monday the week don't ve to change
The simplest way would be to use strtotime. It can do date calculations based on a textual representation of the delta:
$mydate = strtotime('+3 weeks');
It also accepts a second parameter, which is a timestamp to start from when doing the calculation, so after you get the offset in weeks, you can pass the new date to a second calculation:
// Get three weeks from 'now' (no explicit time given)
$mydate = strtotime('+3 weeks');
// Get the Monday after that.
$mydate = strtotime('next Monday', $mydate);
See strtotime documentation for more examples of notations that you can use.
I would highly recommend using PHP's built-in DateTime class for any date and time logic. It's a much better API than the older date and time functions and creates much cleaner and easier to read code.
For example:
// Current date and number of weeks to add
$date = '2015/07/15';
$weeks = 3;
// Create and modify the date.
$dateTime = DateTime::createFromFormat('Y/m/d', $date);
$dateTime->add(DateInterval::createFromDateString($weeks . ' weeks'));
$dateTime->modify('next monday');
// Output the new date.
echo $dateTime->format('Y-m-d');
References:
DateTime.
DateTime::createFromFormat
DateTime::add
DateTime::modify
DateInterval::createFromDateString
DateTime::format
Are you looking for something like this?
$today = time();
$weeks = 2;
// timestamp 2 weeks from now
$futureWeeks = strtotime("+ ".$weeks." weeks");
// the next monday after the timestamp date
$futureMonday = strtotime("next monday",$futureWeeks);
echo date("Y-m-d", $futureMonday);
// or in one line
echo date("Y-m-d", strtotime("next monday", strtotime("+ ".$weeks." weeks")));
PHP is using an unix timestamp for date calculations. Functions as date() and strtotime() using a timestamp as an optional second parameter. This is used a reference for formatting and calculations. If no timestamp is passed to the function the current timestamp is used (time()).
I have the answer here. This will show the next wednesday every 2 weeks and the first date to start from would be the 10th.
I have also added in an estimated delivery which would be 6 weeks after that date.
We will be placing our next order for this on:
<?php
$date = '2020/05/26';
$weeks = 2;
$dateTime = DateTime::createFromFormat('Y/m/d', $date);
$dateTime->add(DateInterval::createFromDateString($weeks . ' weeks'));
$dateTime->modify('wednesday');
echo $dateTime->format('d/m/Y');
?>
Expected delivery for the next order will be:
<?php
$date = '2020/05/26';
$weeks = 2;
$dateTime = DateTime::createFromFormat('Y/m/d', $date);
$dateTime->add(DateInterval::createFromDateString($weeks . ' weeks'));
$dateTime->modify('+42 days next wednesday');
echo $dateTime->format('d/m/Y');
?>
If anyone can confirm this is correct that would be great.
In my Mysql table I have a timestamp field.
In my php page I show the record ($row[data]) in this way
echo date("d-m-Y H:i:s", strtotime($row['data']));
Now in my php I'd like to add 1 hour on my date.
How can I do this?
Thanks
echo date("d-m-Y H:i:s", strtotime($row['data'])+3600);
note that you can't compare resulting dates in "d-m-Y" format.
either compare strtotime results or initial dates. or format your results into proper format first.
You could do this straight away in MySQL:
SELECT `date` + INTERVAL 1 HOUR AS `date`
FROM ...
echo date("d-m-Y H:i:s", strtotime($row['data'].' +1 hour'));
Try with:
strtotime($row['data'] . ' +1 hour')
echo date("d-m-Y H:i:s", strtotime($row['data']) + 3600);
Just add the time manually (3600 seconds = 1 hour).
Add 3600 seconds and you will be fine.
echo date("d-m-Y H:i:s", strtotime($row['data'])+3600);
I have a fixed date from MySql
startDate = 07/03/2011
I wanted to add 60 days on top this date to have an endDate.
$startDate = $result['startDate'];
$endDate = ??? + strtotime("+60 days");
echo $endDate;
From my research, I know it has something do with strtotime, but all the sites I come across with based the start date from current workstation's time. My date is already fixed and entered prior to running and getting the endDate.
Help? Thanks in advance!
In addition to PHP solutions others are providing, you can create the endDate right inside of MySQL and save yourself some of the trouble:
SELECT startDate, DATE_ADD(startDate, INTERVAL 60 DAY) AS endDate FROM table;
-- Or by months (not exactly the same thing)
SELECT startDate, DATE_ADD(startDate, INTERVAL 2 MONTH) AS endDate FROM table;
Relevant documentation here...
You could reformat the results of strtotime()
$startDate = $result['startDate']; // 07/03/2011
$endDate = date("m/d/Y", strtotime("$startDate +60 days"));
Demo: http://codepad.org/9rWnoeQb
$startDate = "07/03/2011";
$endDate = strtotime("+60 days",time($startDate));
$formatted = date('m/d/Y',$endDate);
echo $endDate . "<br/>" . $formatted;
86400 seconds in a day, times number of days.. and add it to current time.
$nextMonth = time()+86400*60;
echo date("Y-m-d H:i:s", $nextMonth);
I want to get the date with specific day and time in PHP, like i want the date of next day and time of 9.30 am i.e "2011-06-02 09:30:00".
the code i was using get to do that,
<?php
$next_day_date = date("Y")."-".date("m")."-".(date("d")+1)." 09:30:00";
$new_trig_time_stamp = strtotime($next_day_date);
$trigger_date_time = date("Y-m-d H:i:s",$new_trig_time_stamp);
echo $trigger_date_time;
?>
the code above works fine but fails on 31 day, on 31st it returns "1970-01-01 05:30:00".
Is there any other way to do so.
When shifting dates by a fixed number, it's better to use mktime(), because it handles invalid dates well (e.g. it knows that January 32 is in fact February 1)
$trigger_date_time = date("Y-m-d H:i:s", mktime(9,30,0, date('n'), date('j')+1, date('Y'));
strtotime() is very useful here.
$trigger_date_time = date( "Y-m-d H:i:s", strtotime( "tomorrow 9:30" ) );
Calculate it via unix timestamp - much less annoyance
<?php
$trigger_date_time = date("Y-m-d 09:30:00",time() + 60*60*24);
echo $trigger_date_time;
?>
echo date('Y-m-d', strtotime('+1 day')) . ' 09:30:30';
Have a look at date_add.
In more detail, something like...
$myDate = new DateTime("Some time");
$myDate->add(new DateInterval("P1D"));
You can then use $myDate->format(…) to extract formatted representations.