I am trying to select a record from specified date
$yesterday = date('Y-m-d',strtotime("-1 days"));
$this->db->get_where('tablename', array('postid' => $dailystat['postid'], 'timestamp >=' => $yesterday));
But i am not getting any record even if there are entries in table. I also want to make sure that query select the only record which was created on specified date.
Any help will be appreciated..
$yesterday = date('Y-m-d',strtotime("-1 days"));
$this->db->get_where('tablename', array('postid' => $dailystat['postid'], 'timestamp >=' => $yesterday, 'timestamp <' => date('Y-m-d')));
If you share your table schema and sample data, I can give you correct answer. Still I can guess you are compairing date string with timestamp.
The code $yesterday = date('Y-m-d',strtotime("-1 days")); will return $yesterday value as '2017-04-10'. But actually in your database you are compairing with timestamp field, which hold the timestamp in numeric value.
You can use php strtotime function to convert any date to respective time stamp. strtotime($yesterday).
Correct Code will be :
$yesterday = date('Y-m-d',strtotime("-1 days"));
$this->db->get_where('tablename', array('postid' => $dailystat['postid'], 'timestamp >=' => strtotime($yesterday)));
Again please make sure, your database field timestamp is storing only date in form of timestamp.
Another solution is, you can use mysql date compare functions.
Related
What I want is to make a database update with codeigniter where I put a value to current timestamp:
My settings array:
$settingsMachineUpdateEvent[] = array(
'id' => $eventId,
'etid' => '5',
'insert_by' => '2',
'modified_date' => $currentTime
);
And my $currentTime:
$currentTime = date_default_timezone_get();
And I send the update like this:
$this->db->update_batch('balance_events', $settingsMachineUpdateEvent, 'id');
My problem is the $currentTime var, I want to assign it the current time stamp so I can update my modified_date column from mysql with the current timestamp value. I also try'ed with NOW(), time(), and assign it directly from array, nothing worked, I will get 0000-00-00 00:00:00 after the update. Where is the issue? Thanks!
Instead of this:
$currentTime = date_default_timezone_get();
try
$currentTime = date('Y-m-d H:i:s'); // for current time
$currentTime = time(); // for current timestamp
Explanation: date_default_timezone_get() is used to get the timezone, but in your case you need the time or timestamp to store in the table.
I am creating an edit form, in which I have that the options are years for birthday, since I just want to change the year not the date and month. In my database, the column type is in date. I can't change the year but it is storing a default value which is 0000-00-00.
Here is the code that I a using.
<?php
$years = array();
$end = date('Y') - 101;
$start = date('Y');
$yearUser = date('Y',strtotime($userData['User']['birthday']));
$counter = 0;
$selectedYear=0;
for($i=$start;$i>$end;$i--){
if($yearUser == $i)
{
$selectedYear = $counter;
}
$counter++;
array_push($years,$i);
}
echo $this->Form->select('birthday',array(
'options' => $years,
'selected' => $selectedYear
));
echo $this->Form->input('birthday',array(
'type' => 'hidden',
'id' => 'birthYear',
'class' =>'user-profile-location',
));
?>
Thank you for some tips, it will be highly appreciated
No, You cant save only year in date column
It says in the MySQL manual that you can store partial dates
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-format
For that you need to change the datatype of column to Varchar(4) then you can easily do that...
Otherwise you have to save complete date then you can get only year by YEAR() function in MYSQL
A DATE field is always going to be a full date. You have to specify a full date.
Obviously, you can format the output of a DATE field With:
SELECT DATE_FORMAT(yourfield,"%Y-%m");
As per description on this web you can use.
INSERT INTO tablename (yourfield) VALUE ('YEAR: Auto CURDATE()', CURDATE() )";
If you can't change your column type on the database then you will have to convert the user input to a ISO date format with Y-m-d, that way you store the full date on the database then you only have to extract the year part from that column when selecting the record on the database:
TO STORE IT ON DATABASE FROM THE USER INPUT
$yearUser = date('Y-m-d',strtotime($userData['User']['birthday']));
Note that PHP will automatically put current month and day value. ie today is: 9-18, plus user selected year lets say 1983, that would be: 1983-09-18
RETRIEVE STORED YEAR VALUE ONLY:
$yearUser = date('Y',Model->birthday));
If you do can change the data type on the database then just simply store it as an integer, since date() function is already returning an integer in that case.
I am working with the DateTime object and have this problem to obtain the activity of a specific day.
In the controller I do the following query:
$date = new \DateTime('today');
$activity = $em->getRepository('MyBundle:myEntity')->findOneBy(array(
'activity_date' => $date
));
Result for this query is null, but when I define the parameter date in this way:
$date = new \DateTime('Wednesday, January 14, 2015');
I get the activity that matches this date. Why doesn't today work?
I believe that commenter #prodigitalson is right. When you say today that is in fact now which is formatted with YYYY-MM-DD HH:MM:SS.
Now, if your DBMS column is of type date, DBMS will first normalize two values and only then proceed with comparing.
If your incoming parameter has a value of 2014-01-14 16:47:20, comparing it to DBMS value of 2014-01-14 00:00:00 will no match the record.
Try the following:
$date = new \DateTime(); // no need for explicit `today`
$date->setTime(0,0,0); // reset hours, minutes and seconds to 0
$activity = $em->getRepository('MyBundle:myEntity')->findOneBy(array(
'activity_date' => $date
));
Will this work?
I'm saving to my database in the column 'time' in this format (user input using datetimepicker):
dateFormat: 'mm-dd-yy',
timeFormat: 'hh:mm tt',
Example: 07-31-2014 03:37 am
I also have a column named 'status'. If the 'time' (the time saved in the column) + 24 hours surpasses the current time() then 'status' should = 'Expired'. Below is my attempt, but I think strtotime is not interpreting my timestamp as it's marking everything as Expired... example: 08-19-2014 02:05 am Expired
if( (strtotime($row['time'] . "+1 days")) < time()) { $row['status'] = 'Expired'; }
Assuming you are getting date from database like in this format "07-31-2014 03:37 am".
You need to convert it and format it as strtotime readable one like below.
list($month,$day,$year,$hour,$minute) = preg_split("/[-\s:]+/", $row['time']);
$row['time'] = $year."-".$month."-".$day." ".$hour.":".$minute.":00";
Try it.
I want to insert a date into the clients table my db schema is below, I want to insert them into the start_day and end_day fields.
I have the below in validations in ClientController.php
If I insert a foreign date_format other than the one defined below I am thrown an error, but if I do insert the correct one it reverts to 0000-00-00. But if I change the field type to text for example the date_format is inserted fine.
$rules = array(
'project_name' => 'required',
'project_brief' => 'required',
'start_day' => array('required', 'date_format:"m-d-Y"'),
'end_day' => array('required', 'date_format:"m-d-Y"')
);
I'm not sure where the problem lies to be honest. I've even tried to convert the time doing the below:
$start_day_old = Input::get('start_day');
$start_day = date("d-m-Y", strtotime($start_day_old));
$project = new Project;
$project->start_day = $start_day
$project->save();
However the results were the same. Does anyone know how I can rectify this issue?
You can't insert a date formated as dd-mm-yyyy in mysql's date field, it should be yyyy-mm-dd, so in your code here
$start_day = date("d-m-Y", strtotime($start_day_old));
Change it to
$start_day = date("Y-m-d", strtotime($start_day_old));
So, if a date is 15-10-2010 then it'll become 2010-10-15 and it's a valid date for date field in the mysql database.