Link to home
Start Free TrialLog in
Avatar of peter_coop
peter_coopFlag for United Kingdom of Great Britain and Northern Ireland

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($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;
Avatar of Shahzad Fateh Ali
Shahzad Fateh Ali
Flag of Pakistan image

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']));
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));
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.
Avatar of peter_coop

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
@rdonline1

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....
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
i have posted code and would appreciate any input. thanks
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
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
Avatar of jrm213jrm213
jrm213jrm213
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Typo alert - the date in the example I gave above SHOULD be 2019 and NOT 2109....

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."
<?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

thank you very much