• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 284
  • Last Modified:

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;
0
peter_coop
Asked:
peter_coop
  • 6
  • 3
  • 2
  • +4
2 Solutions
 
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 6
  • 3
  • 2
  • +4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now