Solved

date not correct

Posted on 2010-08-30
17
271 Views
Last Modified: 2013-12-13
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
Comment
Question by:peter_coop
  • 6
  • 3
  • 2
  • +4
17 Comments
 
LVL 9

Expert Comment

by:Shahzad Fateh Ali
ID: 33557003
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
 
LVL 11

Expert Comment

by:Rajesh Dalmia
ID: 33557006
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
 
LVL 14

Expert Comment

by:svgmuc
ID: 33557017
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
 

Author Comment

by:peter_coop
ID: 33557110
@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
 

Author Comment

by:peter_coop
ID: 33557125
@rdonline1

outputs -> 2013-10-10

this is the only date i am having trouble with. all other date functions are fine.
0
 
LVL 9

Expert Comment

by:Shahzad Fateh Ali
ID: 33557133
did you check your server time and tz configurations?
0
 
LVL 11

Expert Comment

by:Rajesh Dalmia
ID: 33557142
r u sure you are not updating this variable $destroydate  in between with any other details?
0
 
LVL 11

Expert Comment

by:Rajesh Dalmia
ID: 33557156
also can check if u have used same $destroydate variable in any calculation by mistake... otherwise it should work....
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:peter_coop
ID: 33557165
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
 

Author Comment

by:peter_coop
ID: 33557318
i have posted code and would appreciate any input. thanks
date-code.txt
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 33557669
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
 

Author Comment

by:peter_coop
ID: 33558064
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
 
LVL 17

Accepted Solution

by:
jrm213jrm213 earned 125 total points
ID: 33558270
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
 
LVL 34

Assisted Solution

by:Beverley Portlock
Beverley Portlock earned 125 total points
ID: 33558401
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
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 33558425
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
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 33560190
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
 

Author Closing Comment

by:peter_coop
ID: 33560510
thank you very much
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now