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.
Related
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.
I've got a jquery datepicker
$(function() {
var date = new Date();
var currentMonth = date.getMonth();
var currentDate = date.getDate();
var currentYear = date.getFullYear();
$('.datepicker').datepicker({
showOn: 'focus',changeMonth: true,
minDate: new Date(currentYear, currentMonth, currentDate),
changeYear: true,
});
});
with some validation
$this->validate($request, [
"date" => 'required|date',
]);
and some conversion for the date to pass through to the database
$date = $request->input('date');
$date2 = DateTime::createFromFormat('m/d/Y',$date);
$date3 = $date2->format("Y-d-m");
If I dd($date3) at this point, with a chosen date, I get something like this
"2016-29-09"
All well and good, mysql appears to save in a y-d-m format.
Now when I go to pass it through
Message::where('id', $messageId)->update([
'updated_at' => Carbon\Carbon::now(),
'subject' => 'Status: Price quote delivered.',
'quoted' => 1,
'price' => $request->input('price'),
'estimated_date' => $date3,
]);
everything gets passed to the database except the date. The date just stays at 0000-00-00 in the database.
Help?
Note that you are saving date as YEAR-DAY-MONTH and the correct format for DATE is YEAR-MONTH-DAY.
Also note that value of $date3 is "2016-29-09", this will insert value in your table as "0000-00-00" due to invalid value of Month and correct range of Month is (1-12) for data type DATE.
Solution:
You need to change the date format (YEAR-MONTH-DAY) as per your Data type of column.
$date3 = $date2->format("Y-m-d"); // something like 2016-09-26
You can also follow the reference:
http://dev.mysql.com/doc/refman/5.7/en/datetime.html
Bro, check this line:
All well and good, mysql appears to save in a y-d-m format.
Now when I go to pass it through
Mysql default format is
Y-m-d
not
y-d-m
Change the format and try again.
All you have to do with this:
From:
$date3 = $date2->format("Y-d-m");
To:
$date3 = $date2->format("Y-m-d");
I see that you are using a javascript date object, then it is validated in Laravel, why don't you use Carbon? If the date is validated, then instead of reinventing the wheel, just let Carbon and Laravel handle all the date work:
$date = new \Carbon\Carbon($request->input('date'));
Message::where('id', $messageId)->update([
'updated_at' => Carbon\Carbon::now(),
'subject' => 'Status: Price quote delivered.',
'quoted' => 1,
'price' => $request->input('price'),
'estimated_date' => $date,
]);
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 have created an events calendar with codeigniters calendar class which you can view here: Events Calendar
I have it set up where the events are showing up on the calendar and when you click "view events" on a particular day, all the events with that start date pull up and are shown in a modal window.
Well... the problem is that unless its the START DATE of a particular event, the modal window details don't pull up. I know this is because i'm saying in my query to pull events where the start date equals a certain date...
I'm kind of stumped on how to modify this to say, "pull all records where this day is ANYWHERE BETWEEN the start and end date of the event.
Do I need to run a while loop or something and loop through each day of the month? Any ideas on an easier way to do this are appreciated.
the start and end dates are set up as 'Y-m-d H:i:s' in the database and the $query_date variable being passed in is 'Y-m-d', which i change to the same format in the first few lines of the function.
function get_list_events($query_date) {
$start_date_start = date('Y-m-d H:i:s', strtotime($query_date.' 00:00:00'));
$start_date_end = date('Y-m-d H:i:s', strtotime($query_date.' 23:59:59'));
$this->db->where('active', 1);
$this->db->where("start_date BETWEEN '$start_date_start%' AND '$start_date_end%'", NULL, FALSE);
$query = $this->db->get('events');
$data = array();
foreach ($query->result() as $row) {
$data[] = array(
'id' => $row->id,
'title' => $row->title,
'description' => $row->description,
'cost' => $row->cost,
'image' => $row->image,
'start_date' => $row->start_date,
'end_date' => $row->end_date,
'venue' => $row->venue,
'venue_address' => $row->venue_address,
'venue_city' => $row->venue_city,
'venue_state' => $row->venue_state,
'venue_zipcode' => $row->venue_zipcode,
'contact_name' => $row->contact_name,
'contact_email' => $row->contact_email,
'contact_phone' => $row->contact_phone,
'contact_website' => $row->contact_website,
'create_date' => $row->create_date,
'active' => $row->active,
);
}
return $data;
}
I guess your start_date column has the DATETIME or the TIMESTAMP data type. If that isn't true, please update your question.
There's a common trap in date-range processing in all kinds of SQL, due to the fact that when you compare a pure DATE with a DATETIME, they hardly ever come out equal. That's because, for example, DATE('2011-07-1') means the same thing as 2011-07-01 00:00:00.
So you need
start_date >= '$start_date_start'
AND start_date < '$start_date_end' + INTERVAL 1 DAY
instead of what you have, which is
start_date BETWEEN '$start_date_start%' AND '$start_date_end%' /*wrong!*/
The second clause with the < ... + INTERVAL 1 DAY picks up all possible times on the last day of your interval.
Edit Now that you've disclosed that you have two DATETIME columns, called start_date and end_date, it sounds like you're looking for items which start on or before a specific date, and end on or after that same date. Try something like this:
WHERE DATE(start_date) <= DATE('$specific_date')
AND DATE(end_date) >= DATE('$specific_date')
The trick on queries like this is to spend the majority of your time thinking through and specifying the results you want. If you do this, the SQL is often perfectly obvious.