Codeigniter 2.1, MySQL - search by date - php

In my DB I have date field with a standard datetime for MySQL (YYYY-MM-DD HH:MM:SS). Trough AJAX request I am getting this time: DD/MM/YYYY
This is the function I had written to search trough DB:
$this->db->get_where('tvprogram', array('DATE(date)' => $date))->result_array();
At the moment this function isn't getting any data. What seems to be wrong here? How can I search and find all entries for specific day?

Use strtotime function to convert the date to correct format:
$date = date('Y-m-d', strtotime($date));
$this->db->get_where('tvprogram', array('DATE(date)' => $date))->result_array();

Please convert first posted date in database format and use with Date function in your searching field like this.
$date = date('Y-m-d', strtotime($search));
$this->db->where('DATE(created)',$date);

Check the format coming out of DATE(date) because it is probably like YYYY-MM-DD and the comparison will always fail.
Go into phpMyAdmin or similar and try SELECT DATE(now()) and double check the date format.
If $date has a format like DD/MM/YYY, then you can reformat it by using:
$date=date('Y-m-d',strtotime(strtr($date,"/","-")));
From the PHP manual (http://www.php.net/manual/en/function.strtotime.php):
Dates in the m/d/y or d-m-y formats are disambiguated by looking at the separator between the various components: if the separator is a slash (/), then the American m/d/y is assumed; whereas if the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed.
To fix the American/European date ambiguity, I simply replace "/" with "-" before the string to time conversion.

Related

Date difference with custom format

I know there's a lot around here but for many articles I read and test this simply don't work..
I'm saving date with ACF like d.m.y and reading too.
When I make this type of comparison it works BUT only for the current month. No matter what I do, this is the working thing I've done that works, any advise please?
if(strtotime(date('d.m.y')) > strtotime($cd_date_from)) {
$output .= ' class="event-passed"';
}
This means that I'm hiding only the event on the table row that the date is older than today (showing today).
If the date is 15.03.19 or 28.03.19 it shows BUT if it's 15.04.19 (other month) it don't.
Thank you in advance
Dates in the m/d/y or d-m-y formats are disambiguated by looking at the separator between the various components: if the separator is a slash (/), then the American m/d/y is assumed; whereas if the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed. If, however, the year is given in a two digit format and the separator is a dash (-), the date string is parsed as y-m-d.
To avoid potential ambiguity, it's best to use ISO 8601 (YYYY-MM-DD) dates or DateTime::createFromFormat() when possible.
http://php.net/manual/en/function.strtotime.php
Use:
if(strtotime(date('Y-m-d')) > strtotime($cd_date_from)) {
$output .= ' class="event-passed"';
}
and pass Y-m-d format values to $cd_date_from
it's much easier to use \DateTimeInterface to work with DateTime in php:
\DateTime::createFromFormat
<?php
$format = 'd.m.y';
$cd_date_from = '12.03.19';
$date = \DateTime::createFromFormat($format, $cd_date_from);
$date->setTime(0,0,0,0);
if (new \DateTime() > $date) {
echo 'hello';
}

PHP: convert date to datetime and insert it into mysql database?

I have an input field in my html page that gets Date from jQuery datepicker.
The format looks like this:
18/02/2017
I have a MYSQL field which is DATETIME
I need to insert the Date above into this MYSQL field.
So i tried this code:
$mysqlDate = date('Y-m-d H:i:s', strtotime($_POST['php_date']));
but the result of $mysqlDate is this:
1970-01-01 01:00:00
Could someone please advise on this issue?
Thanks in advance.
Do one thing,
$mysqlDate = date('Y-m-d H:i:s', strtotime(str_replace("/","-",$_POST['php_date'])));
echo $mysqlDate;
Give it a try,
it should work.
Note:
Dates in the m/d/y or d-m-y formats are disambiguated by looking at the separator between the various components: if the separator is a slash (/), then the American m/d/y is assumed; whereas if the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed. If, however, the year is given in a two digit format and the separator is a dash (-, the date string is parsed as y-m-d.
Source link.
Your new concern answer,
$mysqlDate = date('Y-m-d H:i:s', strtotime(str_replace("/","-",$_POST['php_date']).' + '.rand(30,60*60*24*3).' seconds'));
You have to convert the posted date to a
YYYY-MM-DD
Format before converting it into date time.
You can do
$date=$_POST['php_date'];
list($day,$month,$year)=explode('/', $date);
$date=$year.'-'.$month.'-'.$day;
And then use $date.

How to check format of Date in PHP?

My SQL server uses the mm/dd/yyyy date format, but the date picker that I have implemented using jQuery gives the date format as dd/mm/yyyy.
So I coded this to check if the given input is in the format of mm/dd/yyyy, but it evaluates to true no matter which format the date input is given in. PHP code is,
$Temp = DateTime::createFromFormat('d/m/Y', $StartsOn);
if($Temp)
$Temp->format('m/d/Y');
I need to convert to mm/dd/yyyy format only if the input is in dd/mm/yyyy. So please tell me what is the logical error that I have made in my code.
it's impossible to reliably check if a date is in dd/mm/yyyy or mm/dd/yyyy format. just think about a date like "May, 7th".
this would be 07/05/2015 or 05/07/2015 depending on the format. so if you just got the date-string with no additional information you can't tell if for example 05/07/2015 is May, 7th or July, 5th.
I am sorry but there is no logical solution to your problem.
From the PHP manual on strtotime
Dates in the m/d/y or d-m-y formats are disambiguated by looking at
the separator between the various components: if the separator is a
slash (/), then the American m/d/y is assumed; whereas if the
separator is a dash (-) or a dot (.), then the European d-m-y format
is assumed. To avoid potential ambiguity, it's best to use ISO 8601
(YYYY-MM-DD) dates or DateTime::createFromFormat() when possible.
The reason passing the date to SQL server works as mm/dd/yyyy is because of the separator. Where possible it is always best to pass as YYYY-MM-DD as per ISO 8601 which was created for exactly this purpose. To fix your problem the best bet is to change the jQuery plugin configuration to output data in that format (if that's not possible, string replace / with - where it's coming from the jQuery plugin. This will avoid future complications by writing code to fix the date format.
You will no be able to tell the difference between mm/dd/yyyy and dd/mm/yyyy when you don't know where it's come from.
You can use a modified version of this function from PHP.net. It uses the DateTime class:
function validateDate($date)
{
$d = DateTime::createFromFormat('d/m/Y', $date);
return $d && $d->format('d/m/Y') == $date;
}
if(validateDate($StartsOn)){
//do job
}
function was copied from this answer or php.net

Current date is failing to format in PHP

I'm using the jquery datepicker, and when today's date is selected, it's failing to convert to the format I need for a database query.
The datepicker date format is 'm-d-Y', so today appears as 09-16-2013.
This part is working, and the datepicker correctly displays today's date as the default selection.
However, I need to use 'Y-m-d' format to query the database. This works fine when the date is less than today. But for today, it's failing and I'm getting 1969-12-31.
echo $enddt;
displays: 09-16-2013
I have tried:
echo date('Y-m-d', strtodate($enddt));
echo date('Y-m-d H:i:s', strtodate($enddt));
and neither works.
How do I format $enddt so it displays 2013-09-16 ?
The function is actually strtotime(), not strtodate():
Dates in the m/d/y or d-m-y formats are disambiguated by looking at the separator between the various components: if the separator is a slash (/), then the American m/d/y is assumed; whereas if the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed. To avoid potential ambiguity, it's best to use ISO 8601 (YYYY-MM-DD) dates or DateTime::createFromFormat() when possible.
Since you have the date string separated by dashes, we'll need to convert it into slashes first to make strtotime() recognize the format as mm/dd/yyyy:
$enddt = '09-16-2013';
$enddt = str_replace('-', '/', $enddt);
echo date('Y-m-d', strtotime($enddt));
Demo!
$enddt = DateTime::createFromFormat('m-d-Y', '09-16-2013');
echo $enddt->format('Y-m-d');
See it in action

Converting php date to fit mysql database

I want to convert an input date in the form of dd/mm/yyyy to the MySQL format which is yyyy-mm-dd.
I was trying to use date('Y-m-d', strtotime($_POST['date'])) but the problem is that the output is always Y-d-m, I think because it considers my 2nd argument to be mm/dd/yyyy.
How do I solve that?
date('Y-m-d', strtotime(str_replace('/', '-', $_POST['date'])))
From the manual:
Dates in the m/d/y or d-m-y formats
are disambiguated by looking at the
separator between the various
components: if the separator is a
slash (/), then the American m/d/y is
assumed; whereas if the separator is a
dash (-) or a dot (.), then the
European d-m-y format is assumed.
You need to convert your delimiters from / to -.
You could do:
$date = implode('-', array_reverse(explode('/', trim($_POST['date']))));
Reference: trim, explode, array_reverse, implode
(trim might not be necessary)
Mohamed,
I would recommend not even formatting the date in the database. If you store all of your date / time values as UNIX TIMESTAMP, you can format the data any way you want after you pull it from the data base.
Here's why: If all of your dates are formatted and you need to compare them, you'd need to bring them back to UNIX TIMESTAMP anyways. Yes, there are wheres to compare formatted date strings but its just one more extra step.
Although this is a bit late : If he uses timestamps then, in my experience he will run into trouble if he tries to perform any MySQL Date arithmetic / calculations on the timestamps - and the over head to do the same in PHP has the potential to become very expensive as it would involve selecting ALL records and then performing comparisons / calculations on the converted dates.
And I concur with jasonbar - PHP is looking at the delimiters of the date and considers it to be a US format date! He will need to run a str_replace('/','-',$_POST['date']) BEFORE using the date() function.
So, to fix this on an incoming request:
$mysqldate = date('Y-m-d', str_replace('/','-',strtotime($_POST['date'])));
So long as the data type for the target column is datetime anyways!
I found a pretty simple conversion.
$YOUR_DATE_FORMAT YYYY/MM/DD
$date = strtotime($YOUR_DATE_FORMAT);
$newdate = date('Y-m-d', $date); //or whatever format you choose.
works like a charm.

Categories