Current date is failing to format in PHP - 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

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.

Wrong date while converting date in php

I used the following code to convert date in php, but I am getting wrong date after the conversion. I used CURRENT_TIMESTAMP as default value in my database table.I am passing "date" variable from javascript.
Can anyone please solve this problem. Thanks in advance.
$date1=mysql_real_escape_string($data->date);
print_r($date1) // This is one displaying the right date.
$date=strtotime($date1);
$timedatenew=date('Y-m-d H:i:s', $date);
print_r($timedatenew) // This is displaying the date like "1970-01-01 01:00:00".
As u said u have date in format dd/mm/yy try with this
$timedatenew= date("Y-m-d H:i:s",strtotime(str_replace('/', '-', $date ))));
it convert / to -
You are getting wrong date because when you use date('Y-m-d',strtotime($date)) for converting date format then 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. Check detail here: php.net/manual/en/function.strtotime.php
(above part is commented by hjpotter92 in same this type of other question)
if you want exact right format then use DateTime object, it can format the date from any string format, it also works well with other formatter other than - like / or .
that means you can change date format from:
d/m/Y or d-m-Y or d.m.Y to your any desired format like Y-m-d.
$date = DateTime::createFromFormat('d/m/Y', "12/07/2019");
echo $date->format('Y-m-d');
// output: 2019-07-12
see below examples from the php official doc DateTime::createFromFormat
Example DateTime::createFromFormat()
Object oriented style
<?php
$date = DateTime::createFromFormat('j-M-Y', '15-Feb-2009');
echo $date->format('Y-m-d');
?>
Procedural style
<?php
$date = date_create_from_format('j-M-Y', '15-Feb-2009');
echo date_format($date, 'Y-m-d');
?>
The above examples will output:
2009-02-15

Codeigniter 2.1, MySQL - search by date

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.

php date format inserting wrong date

I am confused as to why this piece of code does not convert a date in the UK format 00/00/0000 to mysql date format 0000-00-00. What i get in the db is 1970-01-01. It is in the date format not datetime so it should work? Thanks
$destroy = mysql_real_escape_string($_POST['destroy']);
$desttempdate=str_replace("-", "/", $destroy);
$destdate = date('Y-m-d', strtotime($desttempdate));
Here are a few options for you to get it working:
Something like this for PHP4:
function date2timestamp($date,$seperator='/'){
if($date!=''){
$dateEx = explode($seperator,$date);
$date = (strlen($dateEx[2])==2?'20'.$dateEx[2]:$dateEx[2]).'-'.$dateEx[1].'-'.$dateEx[0].' 00:00:00';
}
return $date;
}
For PHP5.3 use createFromFormat():
$date = DateTime::createFromFormat('j/M/Y', $UK_date);
echo $date->format('Y-m-d');
Also other options for createFromFormat() in PHP5.2 include:
PHP createFromFormat for 5.2 version
php dateTime::createFromFormat in 5.2?
DateTime::createFromFormat in PHP < 5.3.0
strtotime() function will return Unix timestamp and 0000-00-00 date does not fit into that - you will just get 0 (zero) instead, which will be converted properly, to 1970-01-01.
You can do what you want by doing something like that:
$destdate = mysql_real_escape_string($_POST['destroy']); // assuming YYYY/MM/DD
$destdate = str_replace('/', '-', $destdate);
if ($destdate != '0000-00-00'){
$destdate = date('Y-m-d', strtotime($desttempdate));
}
The problem is that you are passing to strtotime what it thinks is an invalid date. The documentation says:
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.
To avoid potential ambiguity, it's best to use ISO 8601 (YYYY-MM-DD)
dates or DateTime::createFromFormat() when possible.
So what's happening here is that you are passing in a string using the / separator, which means that strtotime tries to parse it as m/d/y instead of d/m/y. Either do not do the replace at all if your date is in the european format, or better yet use DateTime::createFromFormat() as the docs suggest.
You can check that this is indeed the problem by checking the return value of strtotime:
$destroy = mysql_real_escape_string($_POST['destroy']);
$desttempdate=str_replace("-", "/", $destroy);
var_dump(strtotime($desttempdate));
This will output "bool (false)", confirming that strtotime fails to parse its input.
Solution:
As mentioned above, prefer DateTime::createFromFormat():
$date = DateTime::createFromFormat('d/m/Y', $_POST['destroy']);
$destdate = $date->format('Y-m-d');
Otherwise, even though I do not recommend this approach, the simplest way to fix the problem would be simply to skip the str_replace:
$destdate = date('Y-m-d', strtotime($_POST['destroy']));

Categories