Date not being saved to mySQL datebase with laravel - php

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,
]);

Related

Select record based on specific date in codeigniter

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.

using strtotime with datetimepicker and adding 1 day

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.

Trouble pulling events between 2 dates in Codeigniter / PHP

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.

laravel-4 how to change date format

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.

How to convert a time format that I got from PHP to a format that jQuery countdown accepts

I have this kind of time format, it is stored in my database.. And I want to convert it to a format that jQuery countdown accepts.. I think jQuery countdown accepts this kind of format
Sun Jan 01 2012 00:00:00 GMT+0800 (Malay Peninsula Standard Time)
But the problem is, my time format is like this:
2011-03-29 00:01:03
In order for jQuery countdown to make a countdown, I need to convert that to that long format.. How to do it?
Here's the website of jQuery countdown
Split your format then create a new Date() and pass it to the countdown constructor:
$dateAndTime = '2011-03-29 00:01:03'.split(" ");
$date = $dateAndTime[0].split("-");
$time = $dateAndTime[1].split(":");
// Date(year, month, day, hours, minutes, seconds)
$(selector).countdown({
since: new Date( $date[0], (intval($date[1]) - 1), $date[2], $time[0], $time[1], $time[2])
});
You don't need it in that long format, that is what is just the format that is output when you try to print a Javascript Date object.
You need to create a Javascript Date object
The native way to do that is like so:
var date = new Date([year], [month], [day]);
Note: the month is zero indexed. i.e. January is 0, February is 1, December is 11.
So if you were spitting this out using php.
$date = new DateTime('2011-03-29 00:01:03');
printf('var date = new Date(%d, %d, %d);',
$date->format('Y'),
$date->format('n') - 1,
$date->format('j'),
$date->format('H'),
$date->format('i'),
$date->format('s')
);
Alternatively you could pass it using json:
json_encode(array(
'year' => $date->format('Y'),
'month' => $date->format('n') - 1,
'day' => $date->format('j')
'hour' => $date->format('H'),
'minute' => $date->format('i'),
'second' => $date->format('s')
));
then create the Date with Javascript:
var date = new Date(json.year, json.month, json.day,
json.hour, json.minute, json.second);

Categories