Help with mktime()

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
werlop
Forum Commoner
Posts: 68
Joined: Sat Mar 22, 2003 2:50 am
Location: /dev/null

Help with mktime()

Post by werlop »

Hi, I need to convert some dates into timestamps, I am using the mktime() funtion.

The code I have tried is as follows:

Code: Select all

<?php
$timestamp = date ("U", mktime( 0,0,0,$_POST[month],$_POST[daynumber],$_POST[year]) );
?>
At first this seemed to work, but on closer inspection it does not work as expected.

If I run the code below after making $timestamp I get

27May2003
1040947200
27 Fri December 2002
30 Sat November 2002

Code: Select all

<?php
	print $_POST[daynumber];
	print $_POST[month];
	print $_POST[year];
	print "<br>";
	print $timestamp;
	print date( " d D F Y", $timestamp );
	print date( " d D F Y ", mktime ( 0,0,0,date("D"),date("F"),date("Y")));
?>
This prints the data used to make the timestamp, then the timestamp itself, then uses the date function to make the date using the timestamp (an error occurs here, the 2 dates don't match) It then prints the current date using the mktime() function, and yet again an error occurs here. Does anybody know what's wrong with this?

If not, could you please explain to me how to get the ordinal suffix for a certain number (eg for 1 it would be st, for 2 it would be nd and for 3 it would be rd, etc) without using a timestamp?

Thanks for any help

David
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

Have a look at http://www.php.net/manual/en/function.strtotime.php and see if that's what you're looking for.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

And (although off-topic) also please read:
http://www.php.net/manual/en/language.t ... rray.donts
and start putting quotes around your element names.

Mac
Mr. Tech
Forum Contributor
Posts: 205
Joined: Tue Feb 11, 2003 4:18 pm
Location: Australia

Post by Mr. Tech »

I just use this:

$newtime = strtotime(28th April 2003);

:D
User avatar
werlop
Forum Commoner
Posts: 68
Joined: Sat Mar 22, 2003 2:50 am
Location: /dev/null

Post by werlop »

Thanks this worked perfectly :D I looked at the manual but for some reason I couldn't find what I wanted (probably lack of coffee).

Also thanks for the bit about arrays!

David
Post Reply