/    Sign up×
Community /Pin to ProfileBookmark

[url]http://www.w3schools.com/PHP/php_date.asp[/url]

[CODE]PHP Date() – Adding a Timestamp

The optional timestamp parameter in the date() function specifies a timestamp. If you do not specify a timestamp, the current date and time will be used.

The mktime() function returns the Unix timestamp for a date.

The Unix timestamp contains the number of seconds between the Unix Epoch (January 1 1970 00:00:00 GMT) and the time specified.
Syntax for mktime()
mktime(hour,minute,second,month,day,year,is_dst)

To go one day in the future we simply add one to the day argument of mktime():

<?php
$tomorrow = mktime(0,0,0,date(“m”),date(“d”)+1,date(“Y”));
echo “Tomorrow is “.date(“Y/m/d”, $tomorrow);
?>

The output of the code above could be something like this:
Tomorrow is 2009/05/12[/CODE]

I dont understand the output .

let me analyse it
we have print of date(“Y/m/d”, $tomorrow);

“Y/m/d” –>gives 2009/05/12.

Now there is another part i.e $tomorrow …..[B][COLOR=”Blue”]where is the print for $tomorrow? and if its not for printing then whats the role of of this $tomorrow here ? i am exactly stuck here .
[/COLOR]
[/B]

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@NogDogSep 16.2009 — $tomorrow would be an integer value defining a number of seconds since the "UNIX Epoch" (1970/01/01-00:00:00GMT), a.k.a. a UNIX timestamp. When date() is given only one argument -- the format string -- it uses the current system time as that timestamp. In your example, the preceding line is going through a bunch of contortions to define $tomorrow to be the UNIX Timestamp integer for "tomorrow", then that value is used as the 2nd argument to date(). It would be a bit simpler to use strtotime() to get "tomorrow":
[code=php]
$tomorrow = strtotime('tomorrow');
echo date('Y/m/d', $tomorrow);

// or even:
echo date('Y/m/d', strtotime('tomorrow'));

[/code]
Copy linkTweet thisAlerts:
@cofactorauthorSep 16.2009 — Hi,I appreciate for the alternative suggestion/examples.

by the way, i become totally confused because you told lot many things which are bouncers.

in simple way, if i understand you correctly ...


(1) date('Y/m/d') prints the current system date , time of my machine. ....correct ?


(2)if i put date('Y/m/d', $tomorrow) does this means ...its a tomorrow date ?

How about if we want to make a date say 29th September 2009 ...do we still be using mktime() function ?

If thats so, then how could i make a absolute date say 29th September 2009 using mktime() ....and print it ?
Copy linkTweet thisAlerts:
@NogDogSep 16.2009 — Hi,I appreciate for the alternative suggestion/examples.

by the way, i become totally confused because you told lot many things which are bouncers.

in simple way, if i understand you correctly ...


(1) date('Y/m/d') prints the current system date , time of my machine. ....correct ?
[/quote]

Yes

(2)if i put date('Y/m/d', $tomorrow) does this means ...its a tomorrow date ?
[/quote]

Yes, assuming that $tomorrow has been assigned a timestamp value for tomorrow's date, as your sample code does via mktime().

How about if we want to make a date say 29th September 2009 ...do we still be using mktime() function ?

If thats so, then how could i make a absolute date say 29th September 2009 using mktime() ....and print it ?[/QUOTE]

[url=http://php.net/strtotime]strtotime[/url]() is much easier for something like that:
[code=php]
$some_day = strtotime('2009-09-29'); // unix timestamp integer
echo date('Y/m/d', $some_day);
[/code]

However, this by itself is silly code by itself, since all you would need to do to output a specific date is to just output it:
[code=php]
echo "2009/09/29";
[/code]

For learning purposes, though, it could be supposed that the timestamp value might have been derived in some programmatic manner and not simply assigned a hard-coded value.
Copy linkTweet thisAlerts:
@cofactorauthorSep 16.2009 — aah...feeling better now ...nice explanation.

two small questions on your comments


(1)

you wrote ,

$some_day = strtotime('2009-09-29'); // unix timestamp integer

echo date('Y/m/d', $some_day);

i think it should have been echo date('Y-m-d', $some_day) instead of echo date('Y/m/d', $some_day) because you are using "-" in '2009-09-29'

I am not sure ..just a wild guess ?


(2) you talked about Unix timestamp $some_day = strtotime('2009-09-29');...and wrote // unix timestamp integer

i still don't understand the importance of this unix timestamp integer thingies...don't know even why we are taking a reference time starting from (1970/01/01-00:00:00GMT)............for me its just a '2009-09-29' string and i am getting a time out of it ...thats it ...nothing much ....may be its deeper concept. but for the time being its not a source stopper to go ahead studying next chapters.....thanks ....that was very much helpful ....that helped me a lot....i liked your post
Copy linkTweet thisAlerts:
@MindzaiSep 16.2009 — aah...feeling better now ...nice explanation.

two small questions on your comments


(1)

you wrote ,

$some_day = strtotime('2009-09-29'); // unix timestamp integer

echo date('Y/m/d', $some_day);

i think it should have been echo date('Y-m-d', $some_day) instead of echo date('Y/m/d', $some_day) because you are using "-" in '2009-09-29'

I am not sure ..just a wild guess ?
[/quote]


No, the posted code is correct and works. You can of course use "-" or whatever else you like in the date format argument of the date function.


(2) you talked about Unix timestamp $some_day = strtotime('2009-09-29');...and wrote // unix timestamp integer

i still don't understand the importance of this unix timestamp integer thingies...don't know even why we are taking a reference time starting from (1970/01/01-00:00:00GMT)............for me its just a '2009-09-29' string and i am getting a time out of it ...thats it ...nothing much ....may be its deeper concept. but for the time being its not a source stopper to go ahead studying next chapters.....thanks ....that was very much helpful ....that helped me a lot....i liked your post[/QUOTE]


Computers only work in numbers, they have no concept of years, months or days. Also think of all the possible different ways a given date could be written. What about the date 3/5/2009 - to me that reads 3rd May 2009, to an American it reads 5th March 2009. It is easier to agree on a standard, simple, numeric format of representing any time. That's the point of a unix timestamp.
Copy linkTweet thisAlerts:
@cofactorauthorSep 16.2009 — thanks ...glad to know this ?
×

Success!

Help @cofactor spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.1,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...