-1

Possible Duplicate:
MySQL date format

i have this date which i need to make a comparison but i need to do it since the query!

Now i have this query:

Select date WHERE date="$today"

Where $todayis in this format :dmY how can i directly transform it into dmY aswell? the date field? Something like:

Select date("dmY",date) WHERE date="$today"

I don't know if this is even possible to do, but i guess it might be, so i'd appreciate a little help even if it's simply, yes, it's doable, no, it's not!

2
  • Don't tell me, aren't people allowed to have the same problems? Apparently i have read things and yet didn't find what my capacity could understand -.-' ! Just because you can vote down, don't means you can use it anytime Oct 4, 2012 at 14:19
  • uh? did I forbid you to have the same problem? Did I actually stopped you from using this site? Incl. using the search and looking around? And you do know that posting a question on site comes with the feature to get both: down or upvotes. I can't understand that you now complain about posting a question here. Ohters might think otherwise, so it can happen you get a downvote even if you don't feel it's right.
    – hakre
    Oct 4, 2012 at 14:22

4 Answers 4

1

I think where you are having issues is formating the php date to fit with what is been formated from your mysql_query

Here's what i came up with.

I have created a simple code for you to handle this.

The Procedure is that i got today's date from mysql using CURDATE() function of mysql and then format it and match the exisiting column on the table

MYSQL DATABASE

    -- phpMyAdmin SQL Dump
-- version 3.4.5
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Oct 04, 2012 at 01:44 PM
-- Server version: 5.5.16
-- PHP Version: 5.3.8

SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";

--
-- Database: `my_db`
--

-- --------------------------------------------------------

--
-- Table structure for table `datetable`
--

CREATE TABLE IF NOT EXISTS `datetable` (
  `dateid` int(2) NOT NULL AUTO_INCREMENT,
  `datestuff` date NOT NULL,
  PRIMARY KEY (`dateid`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;

--
-- Dumping data for table `datetable`
--

INSERT INTO `datetable` (`dateid`, `datestuff`) VALUES
(1, '2012-10-02'),
(2, '2012-10-04');

PHP CODE (FILE)

   <?php
//Connect to your host
$con = mysql_connect("localhost","admin","admin");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
//select your database
mysql_select_db("my_db", $con);

//getting today's date and formating it, output is like this (4-10-2012)
$today = date('j-m-Y');

//query your table, also date has been formated like this (4-10-2012)
$result = mysql_query("SELECT DATE_FORMAT( CURDATE( ) ,  '%e-%m-%Y' ) AS today, DATE_FORMAT( datestuff,  '%e-%m-%Y' ) AS datestuff FROM datetable WHERE CURDATE( ) = datestuff");

//get the resultset
while($row = mysql_fetch_array($result))
  {
  echo "date in table: ".$row['datestuff']. "---- today's date: ". $row['datestuff'];
  echo "<br />";
  }
?>

Try it out let me know if it worked out for you....

5
  • even this is very kind of you, you should not do all this just to answer a somewhat standard questions that might even be a duplicate.
    – hakre
    Oct 4, 2012 at 14:11
  • Hey thanks for this but it won't work.. I basically need to compare dates.. Oct 4, 2012 at 14:12
  • @hakre dude i was just trying to help a brother out...
    – Oluwaseye
    Oct 4, 2012 at 14:30
  • sure, I don't stop you, just didn't wanted you feel wasted early.
    – hakre
    Oct 4, 2012 at 14:32
  • @BlertaBlerta the code i pasted already does that, look at it well. i got the current date, compared it with a date row in the table and used it to fetch results...isn't that what you want to do ?
    – Oluwaseye
    Oct 4, 2012 at 14:34
1
Select date_format(date, '%d%m%Y') 
from your_table 
WHERE date="$today"

See MySQL DATE_FORMAT

1
  • Unfortunately, nobody can agree on date format strings. The PHP one is certainly the most flexible, and I wish MySQL and C# could support it.
    – Patrick
    Oct 4, 2012 at 13:01
1

you need to use date_format

Select date_format(date, '%d%m%Y) from table_name where date = "$today"

0
Select date_format("2012-06-01",'%d-%m-%Y') WHERE date="$today"

Note * : You can use date separator "-" or "/" in "%d/%m/%Y

Compare date Example :

Select * From temp where mydate > '2009-06-29 16:00:44';
0

Not the answer you're looking for? Browse other questions tagged or ask your own question.