Page 1 of 1

Mail, headers, date problem

Posted: Thu Apr 10, 2008 1:22 am
by nakkeli
Hi,

I am trying to create an emailer page, and I cannot seem to get the date right. Here's my code:

Code: Select all

 
<html>
<body>
<?php
 
$to = "woo@hoo.com"; // Change this to your e-mail address ;)
$subject = "PHP mail message";
$message = "This mail was sent using PHP mail funtion.";
 
$headers = 'From: Woo Hoo <woo@hoo.com>' . "\r\n";
$headers .= 'Date: ' . date ( 'r' ) . "\r\n";
 
if (mail ($to, $subject, $message, $headers)) {
    echo "Success.<br />\n";
}
else {
    echo "Failure.<br />\n";
}
 
?>
</body>
</html>
 
I have tried various date function styles as well as changing the whole $headers line. Every time I just get the current time as date or 1.1.1970 for date.

Thanks in advance.

Re: Mail, headers, date problem

Posted: Thu Apr 10, 2008 2:07 am
by Benjamin
This returns the date in the format YYYY-MM-DD HH:MM:SS. You can remove the " H:i:s" to remove the time portion. Hope that helps.

Code: Select all

 
date("Y-m-d H:i:s")
 

Re: Mail, headers, date problem

Posted: Thu Apr 10, 2008 2:28 am
by nakkeli
Thank you for your fast reply. Unfortunately, it was the same result as always. Date was 1.1.1970 4:23. :cry:

Re: Mail, headers, date problem

Posted: Thu Apr 10, 2008 2:33 am
by Benjamin
Try setting the timezone:

Code: Select all

 
date_default_timezone_set('America/Chicago');
 
Although I'm not sure that will fix it. You may want to post more of your code, along with the time indicated by the server and your PHP version.

Re: Mail, headers, date problem

Posted: Thu Apr 10, 2008 2:35 am
by Chris Corbyn
Sounds like the time is not set correctly on the server :?

What does this show you?

Code: Select all

var_dump(time());

Re: Mail, headers, date problem

Posted: Thu Apr 10, 2008 2:46 am
by nakkeli
That's the core code of my mailer page, it has all kinds of logins and list functions because it's a newsletter sender. I don't think pasting more code here would help...

I tried to set the timezone to Europe/Helsinki, it didn't help:
date_default_timezone_set('Europe/Helsinki');

var_dump(time()); shows:
int(1207813475)

Re: Mail, headers, date problem

Posted: Thu Apr 10, 2008 2:57 am
by Chris Corbyn
Ok, and:

Code: Select all

date("Y-m-d H:i:s", 1207813475);
? (That's roughly speaking what date() without the second parameter should return).

Re: Mail, headers, date problem

Posted: Thu Apr 10, 2008 2:59 am
by Benjamin
For reference I get:

Code: Select all

 
(.)(.)> php -r 'echo date("Y-m-d H:i:s", 1207813475);'
2008-04-10 02:44:35
 

Re: Mail, headers, date problem

Posted: Thu Apr 10, 2008 3:18 am
by nakkeli
Code:

Code: Select all

 
echo date("Y-m-d H:i:s", 1207813475) . "<br />";
echo date("Y-m-d H:i:s");
 
Result:
2008-04-10 10:44:35
2008-04-10 11:14:37

But still, even if I use these in my mailer code, for example:

Code: Select all

 
$headers .= 'Date: ' . date("Y-m-d H:i:s", 1207813475) . "\r\n";
 
The date in the mail is 1.1.1970 4:23, or similar.