peter_coop
asked on
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($ac t,$i,"dest roydate")) ); for displaying on page.
however, if i do:
$destroydate = date('Y-m-d', strtotime($_GET['destroyda te'] )); 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;
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($ac
however, if i do:
$destroydate = date('Y-m-d', strtotime($_GET['destroyda
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;
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 ));
$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
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.
ASKER
@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
echo date('Y-m-d',strtotime('08
@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
ASKER
@rdonline1
outputs -> 2013-10-10
this is the only date i am having trouble with. all other date functions are fine.
outputs -> 2013-10-10
this is the only date i am having trouble with. all other date functions are fine.
did you check your server time and tz configurations?
r u sure you are not updating this variable $destroydate in between with any other details?
also can check if u have used same $destroydate variable in any calculation by mistake... otherwise it should work....
ASKER
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
$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
ASKER
i have posted code and would appreciate any input. thanks
date-code.txt
date-code.txt
I have an article here at EE that tells the ways to handle DATETIME strings in PHP and MySQL
https://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
https://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
ASKER
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Typo alert - the date in the example I gave above SHOULD be 2019 and NOT 2109....
like so...
strtotime( str_replace("-", "/", "08-31-2019") );
like so...
strtotime( str_replace("-", "/", "08-31-2019") );
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."
"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";
ASKER
thank you very much
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
echo date('Y-m-d',strtotime($_G