[thelist] Convert string to date

mia at miaridge.com mia at miaridge.com
Wed Jul 10 05:49:00 CDT 2002


On Wed, 10 Jul 2002, ~kristina wrote:

> How is
> deleteday
> deletemonth
> deleteyear
> converted to a proper date

If you're going to convert those into a date, you need to build a string
to give to the function that makes a date from a string first.  I've given
an example below.

You can use strtotime() (http://www.php.net/strtotime) or mktime()
(http://www.php.net/mktime) with time() to get the current time, or date.
There's a list of acceptable date formats at
http://www.gnu.org/manual/tar-1.12/html_chapter/tar_7.html you can check
if your string isn't working.

Have you looked up the function definitions at php.net to check what input
(arguments) you should be using, and what you should be getting back from
the function?  You can get information about any function by going to
http://www.php.net/functionname.

You'd want to check the format, but once I've got the my date into date
format, I use date() to format it again and compare it to today.

(Probably horribly inefficient) example:

// get today's date, format it for comparison
$today = date("j n Y", time())

// get the data from the form usable, format it for comparison
$expirydate = mktime(0,0,0, $deletemonth, $deleteday, $deleteyear);
$expirydate = date("j n Y", $expirydate);

// then use it like this
if ($today == $expirydate) {
	// This is from your example
	// but note that you don't need '*' in a DELETE
	$query = "DELETE FROM tableName WHERE expirydate=$expirydate";

	// uncomment the line below to print your query to the
	// screen for easier testing
	//echo $query;

	// whatever database calls you need to send the query
}

> When I created the table I did this (which I'm not sure is correct
> either)

Have you got phpMyAdmin?  If not, I'd recommend it.  It's a web interface
to your mysql databases, it makes creating and managing database really
easy, and you can use it to test your queries too.  It's a big sanity
saver when you want to check if a SQL command worked.

You can get phpMyAdmin from http://www.phpmyadmin.org/.

cheers, Mia





More information about the thelist mailing list