date not correct

hi
how do i convert php date to mysql date based on php variable? for example, if i use this code:

$destroydate = $_GET['destroydate']; it displays 08-11-2019 which is being passed from the previous page with this code:

$destroydate = date('d-m-Y', strtotime(mysql_result($act,$i,"destroydate"))); for displaying on page.

however, if i do:
$destroydate = date('Y-m-d', strtotime($_GET['destroydate'] )); it shows a date of
2014-05-24 which is nothing like the date i passed. what is the correct way to format  this for inclusion in mysql?. many thanks;
peter_coopAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Shahzad Fateh AliWeb Solutions Architect & Technical Project Manager- VentureDive (Pvt) LtdCommented:
Hi,

The way you are using strtotime is correct. Check your server time, and timezone settings. Also try to debug it and see that your inputs are correct.
I did a little test like:

echo date('Y-m-d',strtotime('08-11-2019'));
echo date('Y-m-d',strtotime($_GET['destroydate']));
0
Rajesh DalmiaCommented:
can try this

$date_array = explode("-",$destroydate ); // split the array
       
$yr = $date_array[0];
$mon = $date_array[1];
$day = $date_array[2];

$destroydate = date('Y-m-d', mktime(0,0,0,$mon,$day,$yr));
0
svgmucCommented:
You could just select DATE_FORMAT(`date_column`, '%Y-%m-%d') in your query, and there would be no need for anymore conversion within the PHP script.
0
Protecting & Securing Your Critical Data

Considering 93 percent of companies file for bankruptcy within 12 months of a disaster that blocked access to their data for 10 days or more, planning for the worst is just smart business. Learn how Acronis Backup integrates security at every stage

peter_coopAuthor Commented:
@shahzadfatehali

echo date('Y-m-d',strtotime('08-11-2019')) outputs 2014-05-12

@svgmuc
sorry. that is not an option

for the record, i am using php 4.4.7 and the mysql field is DATE. thanks

@rdonline1
shall try your idea now

many thanks
0
peter_coopAuthor Commented:
@rdonline1

outputs -> 2013-10-10

this is the only date i am having trouble with. all other date functions are fine.
0
Shahzad Fateh AliWeb Solutions Architect & Technical Project Manager- VentureDive (Pvt) LtdCommented:
did you check your server time and tz configurations?
0
Rajesh DalmiaCommented:
r u sure you are not updating this variable $destroydate  in between with any other details?
0
Rajesh DalmiaCommented:
also can check if u have used same $destroydate variable in any calculation by mistake... otherwise it should work....
0
peter_coopAuthor Commented:
as i said all other dates are returning fine zones and config are fine. sure. if i echo

$destroydate = $_GET['destroydate']; it shows the correct date in the wrong format: d-m-Y. if i add any date functions to it, it totally throws the date out of sync. would it help if i posted the code that makes the var in the first place and subsequent code thereafter? thanks
0
peter_coopAuthor Commented:
i have posted code and would appreciate any input. thanks
date-code.txt
0
Ray PaseurCommented:
I have an article here at EE that tells the ways to handle DATETIME strings in PHP and MySQL
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201.html

What you want to learn about is called the ISO8601 DATETIME format.
http://www.google.com/#hl=en&source=hp&q=ISO8601

Use that format (which is PHP date('c') in most installations) for ALL internal representations of the date.  Use urlencode() to pass the date in the GET string.

Please read the article and post back with any questions, ~Ray
0
peter_coopAuthor Commented:
ray, thank you for that. however, do you have a practical example of urlencode. i am using other date functions and they are working ok without using urlencode> thanks
0
jrm213jrm213Commented:
Hi,

strtotime is returning some strange results for me on testing it in the way you are using it. For example:

strtottime('08-11-2019') is returning: 1573189200 which is expected but
strtottime('08-31-2019') is returning: 0 which is not expected.

Changing the dashes to slashes resolves the issue for me with all dates that I try, while using dashes I get mixed results.
 
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Beverley PortlockCommented:
All the formats strtotime take are listed here http://uk3.php.net/manual/en/datetime.formats.date.php

mm-dd-yyyy is not on the list so convert it to another format. mm/dd/yyyy is on the list so

strtotime( str_replace("-", "/", "08-31-2109") );

should work. The reason that strtottime('08-11-2019') appears to work is because dd-mm-yyyy is an allowed format and strtotime thinks it is the 8th Nov 2109.

Also be advised that strtotime does not work for dates after 2038

0
Beverley PortlockCommented:
Typo alert - the date in the example I gave above SHOULD be 2019 and NOT 2109....

like so...

strtotime( str_replace("-", "/", "08-31-2019") );
0
Ray PaseurCommented:
Fortunately all of the PHP functions are documented on the PHP.net web site.

"This function is convenient when encoding a string to be used in a query part of a URL, as a convenient way to pass variables to the next page."
<?php // RAY_temp_peter_coop.php
error_reporting(E_ALL);
echo "<pre>" . PHP_EOL;

// DEMONSTRATE URLENCODE()
// MAN PAGE HERE: http://us.php.net/manual/en/function.urlencode.php

// SOMETHING THAT DOES NOT REALLY NEED TO BE URLENCODED...
$x = 'RAY';
$y = urlencode($x);
echo PHP_EOL . $y . " = URLENCODE OF $x";

// SOMETHING THAT NEEDS TO BE URLENCODED...
$x = 'RAY PASEUR';
$y = urlencode($x);
echo PHP_EOL . $y . " = URLENCODE OF $x";

// SOMETHING ELSE THAT NEEDS TO BE URLENCODED...
$x = date('c');
$y = urlencode($x);
echo PHP_EOL . $y . " = URLENCODE OF $x";

Open in new window

0
peter_coopAuthor Commented:
thank you very much
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.