Page 1 of 1

date problem

Posted: Thu Jan 11, 2007 10:51 am
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?

Posted: Thu Jan 11, 2007 11:01 am
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>";

Posted: Thu Jan 11, 2007 11:04 am
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

Posted: Thu Jan 11, 2007 11:05 am
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?

Posted: Thu Jan 11, 2007 11:09 am
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

Posted: Thu Jan 11, 2007 11:12 am
by John Cartwright
Your server is likely in a different timezone than yourself

Posted: Thu Jan 11, 2007 11:13 am
by feyd
hint: The strtotime() line uses a single quote string. ;)

Posted: Thu Jan 11, 2007 11:33 am
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:

Posted: Thu Jan 11, 2007 11:44 am
by speedy33417
So, what are guys saying? Can it be fixed or how do I deal with it a the best way?

Posted: Thu Jan 11, 2007 11:52 am
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.

Posted: Thu Jan 11, 2007 12:15 pm
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?

Posted: Thu Jan 11, 2007 12:55 pm
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.