Hi
I am trying to get the first day of a given month and the last day. I use mktime function like so:
mktime(0,0,0,7,1,2007);

and got the time stamp: 1183212000 when I converted it I got:
Sat, 30 Jun 2007 14:00:00 GMT which is not what I want, I wanted 1 July.

when I used the date function with mktime like this:
date("M-d-Y", mktime(0,0,0,7,1,2007));

I get a correct value, what am I doing wrong?

Member Avatar for diafol

You need to use special formatting characters:

date("F",mktime(0,0,0,7,1,2007));

Have a look at the date() function in the online php manual for a full list of characters.

I had a look at the date function and tried to use "U" in order to get the timestamp, but the I get the same result.
I want basically to get a day in a certain month in a certain year to be in a timestamp and not the proper date.

mktime likely uses a more complex algorithm for computing the timestamp, it would have to include leapyears for example. If you want to let PHP handle dates, use it for creating the timestamp and for decoding it. If you want to use your own algorithm, you must use that at both ends instead. Mix-and-matching won't work.

Member Avatar for diafol

Sorry khr, I misread your post, if you want 1 July from a mktime() , use the following:

date("j F",mktime(0,0,0,7,1,2007))

There's no real point using 'U' because mktime returns the timestamp anyway. You only need to use 'U' if you want to return the timestamp for the current date: date("U") .
Another use would be to add time to the timestamp to get a future or past date. I'll make it step-wise so you follow it. This process can be shortened considerably:

$days_from_now = -6; //6 days ago

$conv_secs = $days_from_now * 60 * 60 * 24; //convert days to secs

$current_timestamp = date("U");

$target_timestamp = $current_timestamp + $conv_secs; 

$new_date = date("d/m/Y",$target_timestamp);

This will output the correct date (UK/Euro format six days ago).


mktime is clever as it deals well with overflows, e.g. date("d/m/Y",mktime(0,0,0,2,29,2009)) should return 01/03/2009.

ok
thanks for your posts, but let me explain my question again; I put this code into my php script:
mktime(0,0,0,7,1,2007), and got the time stamp: 1183212000.

Use this timestamp converter and see what happens: http://www.onlineconversion.com/unix_time.htm

the mktime suppose to give me 1 july 2007 but rather I get
Sat, 30 Jun 2007 14:00:00 UTC

an another word the mktime gives me the wrong date.
I hope my question is clear now

I hate to suggest something so drastic, but you may want to install another version of PHP. But even if mktime is incorrect, if you are only doing relative comparisons and date fixes mktime's mistake (somehow) does it really matter?

Member Avatar for diafol

Is your server date OK?
do this on a page before mktime calls:

date_default_timezone_set('UTC');

That should help.

Hi,

I AM HAVING A PROBLEM IN VALIDATING TODAYS DATE WITH USER INPUT DATE. MY USER INPUT DATE IS IN HTML SELECT TAGS SUCH AS <SELECT><OPTION>1</OPTION>......</SELECT>AND FOR MONTH AND YEAR ALSO. WHEN I COMPARES THE CURRENT DATE WITH USER INPUT DATE . IT THROWS AN AS PER CODE ,BUT IF I SELECT THE PAST DATE TO ALLOW. WHAT I NEED IS " A CODE THAT COMPARES THE USER INPUT DATE WITH SRVER DATE" .IF THERE IS PAST OR FUTURE, IT SHOULD THROW AN ERROR, ELSE INPUT THE DATA TO ENTER
HERE IS CODE :

$today = date("U");
							$date1 = mktime(0, 0, 0, $month, $day, $year);
							if ($date1 > $today){
      	$ARE U GOOD= false;
							 echo"Enter Correct Date";
							 							}
							}

if any one can fix this problem , reply me

Member Avatar for diafol

Should be ok I think - but what's that variable $ARE U GOOD= false ???

its some thing like $isinsert=true otherwise false.
Can u fix the problem? ya its partially works. It compares the input DATA and throw me the error if its > PRESENT YEAR .It works for the current year and not for current date ? Can u help me

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.