Mail, headers, 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
nakkeli
Forum Newbie
Posts: 15
Joined: Thu Apr 10, 2008 1:18 am

Mail, headers, date problem

Post 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.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Mail, headers, date problem

Post 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")
 
User avatar
nakkeli
Forum Newbie
Posts: 15
Joined: Thu Apr 10, 2008 1:18 am

Re: Mail, headers, date problem

Post by nakkeli »

Thank you for your fast reply. Unfortunately, it was the same result as always. Date was 1.1.1970 4:23. :cry:
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Mail, headers, date problem

Post 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.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Mail, headers, date problem

Post 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());
User avatar
nakkeli
Forum Newbie
Posts: 15
Joined: Thu Apr 10, 2008 1:18 am

Re: Mail, headers, date problem

Post 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)
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Mail, headers, date problem

Post 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).
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Mail, headers, date problem

Post 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
 
User avatar
nakkeli
Forum Newbie
Posts: 15
Joined: Thu Apr 10, 2008 1:18 am

Re: Mail, headers, date problem

Post 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.
Post Reply