date problem

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
speedy33417
Forum Contributor
Posts: 128
Joined: Sun Jul 23, 2006 1:14 pm

date problem

Post by speedy33417 »

I'm getting the wrong day of week for some reason with this code.

Code: Select all

$year = date("Y");
$month = date("m");
$day = date("d");

$dateCheck = $year . "-" . $month . "-" . $day;
$timestamp = strtotime('$dateCheck');
$dayofWeek = date('D', $timestamp);
echo $dayofWeek . "<br>";
echo $year . "-" . $month . "-" . $day;
The result I get is:

Code: Select all

Wed
2007-01-11
The problem is, today's Thursday...

Why am I getting Wednesday?
User avatar
dhrosti
Forum Commoner
Posts: 90
Joined: Wed Jan 10, 2007 5:01 am
Location: Leeds, UK

Post by dhrosti »

surely this is an easier way of doing the same thing...

Code: Select all

echo "<p>".date('D')."<br />".date('Y-m-d')."</p>";
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

I would agree, you don't need to create a unix time stamp then convert it to the day of week....but that is odd that it returns Wed 8O
User avatar
speedy33417
Forum Contributor
Posts: 128
Joined: Sun Jul 23, 2006 1:14 pm

Post by speedy33417 »

That's not my main concern, because I need the those date values for later, however why am I getting Wed instead of Thu?
User avatar
dhrosti
Forum Commoner
Posts: 90
Joined: Wed Jan 10, 2007 5:01 am
Location: Leeds, UK

Post by dhrosti »

Just been reading a little bit, there some problem with calulating when midnight is with strtotime()

See the manual...

http://uk2.php.net/manual/en/function.strtotime.php
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Your server is likely in a different timezone than yourself
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

hint: The strtotime() line uses a single quote string. ;)
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

Some (maybe useful) trivia:

Jan 1, 1970 started with timestamp '0000000000'.

'-1' and 'NULL' and 'error' are often synonymous.

December 31, 1969 was a Wednesday.

:wink:
User avatar
speedy33417
Forum Contributor
Posts: 128
Joined: Sun Jul 23, 2006 1:14 pm

Post by speedy33417 »

So, what are guys saying? Can it be fixed or how do I deal with it a the best way?
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

Let's break down what it is you're doing:

You get the current year.

You get the current month.

You get the current day-of-the-month.

Then you glob them together, try to convert them to a timestamp, and use that timestamp to seed another date() function do generate the weekday.

Seems a rather odd way to go about things, but it would work if you paid closer attention to the strtotime() function.
User avatar
speedy33417
Forum Contributor
Posts: 128
Joined: Sun Jul 23, 2006 1:14 pm

Post by speedy33417 »

So, it looks like that I get a Wed back because there's an error in putting my dates together.
This works:

Code: Select all

$timestamp = strtotime('2007-01-11 16:09:30');
echo date('D', $timestamp);
It retuns Thu lke it should, but this doesn't work:

Code: Select all

$year = '2007';
$month = '01';
$day = '11';
$timestamp = $year . '-' . $month . '-' . $day . ' 16:09:30'; 
echo date('D', $timestamp);
It returns Wed no matter what date I give those values.

I understand that the method I'm doing it is odd, but the date is passed to a link and generates a new calendar week based on that.

Code: Select all

<a href=\"calendar.php?year=" . $year . "&month=" . $month . "&day=" . $day . "\" class=\"text1\">Next week</a>
So how do I use $year, $month and $day to generate a valid timestamp?
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

a UNIX timestamp is the number of seconds since Jan 1, 1970.

strtotime() will make one for you, but watch your quotes. strtotime() will acccept the contents of a variable, but has no idea what the name of a variable means.
Post Reply