LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-19-2007, 01:48 PM   #1
waelaltaqi
Member
 
Registered: Sep 2005
Location: USA, TN
Distribution: CentOS & Ubuntu for Desktop
Posts: 454

Rep: Reputation: 31
PHP Days of the month counter.


I would like to know if there is a way to make PHP count the days in the current month. I'm just starting to learn PHP and it's kind of difficult at first. I will continue my research.
Any help on this will be appreciated.
 
Old 10-19-2007, 04:48 PM   #2
vxc69
Member
 
Registered: Jul 2004
Distribution: Ubuntu
Posts: 387

Rep: Reputation: 33
Quote:
Originally Posted by waelaltaqi View Post
I would like to know if there is a way to make PHP count the days in the current month. I'm just starting to learn PHP and it's kind of difficult at first. I will continue my research.
Any help on this will be appreciated.
Hello,

I think I can help out. But what do you mean by counting the days in the current month? Do you mean whether the current month has 30/31 days in it or do you mean finding out which day of the month it is?



vxc
 
Old 10-19-2007, 05:50 PM   #3
waelaltaqi
Member
 
Registered: Sep 2005
Location: USA, TN
Distribution: CentOS & Ubuntu for Desktop
Posts: 454

Original Poster
Rep: Reputation: 31
thanks a lot ... i think i figured it out ..
i needed to find out the the number of days for the any given current month. Let's say it's April, then i need to PHP to figure out how many days are in the current month "April".
I read a little bit and i found out that i can use the date(t) function to do the job.
 
Old 10-19-2007, 08:15 PM   #4
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
waelaltaqi, you have been around here long enough to know that it is nice to tell other people *how* you solved the problem. I assume you wanted to continue your project and forgot to elaborate.

Actually, the PHP recommended way is to use an expression like:

PHP Code:
$tomorrow  mktime(000date("m")  , date("d")+1date("Y")); 
And in your case I would suggest:
PHP Code:
$last_day  date("M"mktime(000date("m")+1  date(1)-1date("Y"))); 
E.q. take the 1st day of the next month and substract one day. from mktime you get back to a date by using the date function.

And an additional comment from my own experience: go to seconds format as soon as possible, do whatever you have to do and go back to date/time only then. Date/time format was made for humans, but not for computers.

jlinkels
 
Old 10-19-2007, 09:09 PM   #5
waelaltaqi
Member
 
Registered: Sep 2005
Location: USA, TN
Distribution: CentOS & Ubuntu for Desktop
Posts: 454

Original Poster
Rep: Reputation: 31
MailWatch report.

Quote:
Originally Posted by jlinkels View Post
I assume you wanted to continue your project and forgot to elaborate.
Well it's kind of a long story to tell. But here you ...
I'm trying to write a PHP script that will send out monthly spam statistics to my clients. In my opnion, it's one of the main featrues that MailWatch is missing. MailWatch team have wrote such a wonderfull piece of software but i wish they would have made that part easier on everybody. my spam server is MailScanner/MailWatch and people who worked on mailscanner know that it depends heavily on MYSQL. I'm trying to give my clients tables for total spam statistics for the month, average month statistics per mailbox and average daily statistics per day.
I'm a PHP super beginner but i was ver glad that i was able to produce some ouput. I'm still stuck on average dailly per mailbox statistics table which why i asked my original question. Here is the script and i would appreciate any advices or adjustments for my mistakes:
Code:
<html>
<head>
<meta name="keywords" content="">
<style type="text/css">
h3 {font-family: sans-serif }
h3.month {font-family: sans-serif ; color:d7060c}
h3.heading {font-family: sans-serif ; color:1c2894}
h4 {font-family: sans-serif }
table {table-layout: automatic}
tr.first {background-color:FFE303; text-align: center ; font-family: sans-serif ; font-weight: bold}
tr.second{background-color:E8E8E8 ; text-align: center ; font-family: sans-serif}
td {padding: 0.2cm}
p {font-family: sans-serif}
</style>
<title>ctiemail.com Spam Report</title>

</head>

<body>
<table>
       <tr>
               <td class="logo"><img src="Logo.jpg"></td>
               <td class="logo">
                       <h3> Monthly Spam Report for mydomain.com</h3><br>
                       <h3 class="month"><?php echo date(F) . " " . date(Y);?></h2>
               </td>
       </tr>
       </td>
</table>


<?php
$con = mysql_connect("localhost","root","mypass");
if (!$con)
 {
 die('Could not connect: ' . mysql_error());
 }

mysql_select_db("mailscanner", $con);
$domain = "mydomain.com";
//All messages query:
$AllMSG = mysql_query("select COUNT(*) as 'AllMSG' from maillog where to_domain= '$domain' AND MONTH(date) = MONTH(CURRENT_DATE);");

//Clean Message query:
$CleanMSG = mysql_query("select COUNT(*) as 'CleanMSG' from maillog where to_domain= '$domain' AND MONTH(date) = MONTH(CURRENT_DATE) AND isspam=\"0\";");

//Total Spam query:
$TSpamMSG = mysql_query("select COUNT(*) as 'TSpamMSG' from maillog where to_domain= '$domain' AND MONTH(date) = MONTH(CURRENT_DATE) AND isspam=\"1\";");

//High Scoring Spam query:
$HSpamMSG = mysql_query("select COUNT(*) as 'HSpamMSG' from maillog where to_domain= '$domain' AND MONTH(date) = MONTH(CURRENT_DATE) AND ishighspam=\"1\";");

//Black Listed Spam query:
$BSpamMSG = mysql_query("select COUNT(*) as 'BSpamMSG' from maillog where to_domain= '$domain' AND MONTH(date) = MONTH(CURRENT_DATE) AND spamblacklisted=\"1\";");

//White Listed Messages:
$WMSG = mysql_query("select COUNT(*) as 'WMSG' from maillog where to_domain= '$domain' AND MONTH(date) = MONTH(CURRENT_DATE) AND spamwhitelisted=\"1\";");

//Virus Message:
$VirusMSG = mysql_query("select COUNT(*) as 'VirusMSG' from maillog where to_domain= '$domain' AND MONTH(date) = MONTH(CURRENT_DATE) AND virusinfected=\"1\";");

//Bad Content Messages:
$BC = mysql_query("select COUNT(*) as 'BC' from maillog where to_domain='$domain' AND MONTH(date) = MONTH(CURRENT_DATE) AND nameinfected=\"1\";");

//Total Mailboxes Protected:
$TMailbox = mysql_query("SELECT COUNT(*) as 'TMailbox' FROM users WHERE username LIKE '%$domain';");

//Current Date
$CD = mysql_query("SELECT CURRENT_DATE AS 'CD';");

?>
<h4>Total Statistics:<h4/>
<table>
 <tr class="first">
  <td>Total Messages</td>
  <td>Total Spam</td>
  <td>Total Clean</td>
  <td>High Scoring Spam</td>
  <td>White Listed Messages</td>
  <td>Black Listed Spam</td>
  <td>Virus Infected</td>
  <td>Bad Content Messages</td>
 </tr>

 <tr class="second">
  <td>
   <?php
       while($row = mysql_fetch_array($AllMSG))
         {
           $AllMessages = $row['AllMSG'];
           echo $AllMessages ;
         }
     ?>
   </td>
   <td>
        <?php
          while($row = mysql_fetch_array($TSpamMSG))
            {
             $TotalSpam = $row['TSpamMSG'];
             $SpamPer = round(($TotalSpam * 100 ) / $AllMessages,1);
             echo $TotalSpam . "<br>" . "(".$SpamPer.")"."%";
            }
        ?>
   </td>
   <td>
       <?php
         while($row = mysql_fetch_array($CleanMSG))
           {
            $AllCleanMessages = $row['CleanMSG'];
            $CleanPer = round(($AllCleanMessages * 100 ) / $AllMessages,1);
             echo $AllCleanMessages . "<br>" . "(".$CleanPer.")"."%";
           }
       ?>
   </td>
   <td>
       <?php
          while($row = mysql_fetch_array($HSpamMSG))
            {
             $HighSpam = $row['HSpamMSG'];
             $HSpamPer = round(($HighSpam * 100 ) / $AllMessages,1);
             echo $HighSpam . "<br>" . "(".$HSpamPer.")"."%";
            }
        ?>
   </td>
   <td>
       <?php
          while($row = mysql_fetch_array($WMSG))
            {
             $WhiteMessages = $row['WMSG'];
             $WhiteMSGPer = round(($WhiteMessages * 100 ) / $AllMessages,1);
             echo $WhiteMessages . "<br>" . "(".$WhiteMSGPer.")"."%";
            }
        ?>
   </td>
    <td>
       <?php
          while($row = mysql_fetch_array($BSpamMSG))
            {
             $BlackSpam = $row['BSpamMSG'];
             $BSpamPer = round(($BlackSpam * 100 ) / $AllMessages,1);
             echo $BlackSpam . "<br>" . "(".$BSpamPer.")"."%";
            }
        ?>
   </td>
    <td>
       <?php
         while($row = mysql_fetch_array($VirusMSG))
           {
            $VirusMessages = $row['VirusMSG'];
            $VirusSpamPer = round(($VirusMessages * 100 ) / $AllMessages,3);
            echo $VirusMessages . "<br>" . "(".$VirusSpamPer.")"."%";
           }
       ?>
   </td>
     <td>
         <?php
         while($row = mysql_fetch_array($BC))
           {
            $BC = $row['BC'];
            $BCPer = round(($BC * 100 ) / $AllMessages,3);
            echo $BC . "<br>" . "(".$BCPer.")"."%";
           }
       ?>


     </td>

</table>

<h4>Monthly Average Statistics (Per Mailbox):</h4>
<table>
 <tr class="first">
 <td>Total Mailboxes Protected </td>
  <td>Total Average</td>
  <td>Spam Messages Average</td>
  <td>Clean Average</td>
  <td>Virus Messages Average</td>
 </tr>

 <tr class="second">
  <td>
   <?php
       while($row = mysql_fetch_array($TMailbox))
         {
           $TotalMailbox = $row[TMailbox];
           echo $TotalMailbox ;
         }
     ?>
   </td>
   <td>
   <?php
       $AVGMonthTotal = round(($AllMessages / $TMailbox),1);
        echo $AVGMonthTotal;
     ?>
   </td>
   <td>
   <?php
       $AVGMonthSpam = round(($TotalSpam / $TMailbox),1);
        echo $AVGMonthSpam;
     ?>
   <td>
   <?php
       $AVGMonthClean = round(($AllCleanMessages / $TMailbox),1);
        echo $AVGMonthClean;
     ?>

   </td>
   <td>
    <?php
       $AVGMonthVirus = round(($VirusMessages / $TMailbox),1);
        echo $AVGMonthVirus;
     ?>
   </td>
</table>
<h4>Daily Average Statistics (Per Mailbox):</h4>
<table>
 <tr class="first">
 <td>Total Mailboxes Protected </td>
  <td>Total Average</td>
  <td>Spam Messages Average</td>
  <td>Clean Average</td>
  <td>Virus Messages Average</td>
 </tr>

 <tr class="second">
  <td>
     <?php
       echo $TotalMailbox ;
     ?>
   </td>
   <td>
   <?php
      $CurrrentDate = day(t);
      $AVGMDayTotal = round(($AVGMonthTotal / $CurrentDate),1);
       echo $AVGDayTotal;
     ?>
   </td>
   <td>
   <?php
       $AVGMonthSpam = round(($TotalSpam / $TMailbox),1);
        echo $AVGMonthSpam;
     ?>
   <td>
   <?php
       $AVGMonthClean = round(($AllCleanMessages / $TMailbox),1);
        echo $AVGMonthClean;
     ?>

   </td>
   <td>
    <?php
       $AVGMonthVirus = round(($VirusMessages / $TMailbox),1);
        echo $AVGMonthVirus;
     ?>
   </td>
</table>

<?php mysql_close($con);?>

</body>
</html>
I'm stuck on creating another average daily table. To calculate the daily average per mailbox, i need to divide the Monthly average per mailbox by the number of days in a given month. Since the script is supposed to be schdualed in a cron job, i can't set the date manually. I need PHP to figure out what is the current day for any month at any given time and set it to a variable and use the variable in my calculation. If i figured it out, will follow the rules and post my new results. As i said, i welcome any additions for the scripts and i believe a lot of mailwatch users will find it usefull. I feel that it could have been written in a less complex platform, but what can i see ... trying to learn some PHP programing.

Best Regards;

Last edited by waelaltaqi; 10-19-2007 at 10:03 PM.
 
Old 10-20-2007, 09:41 AM   #6
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
Is it really the number of days in the current month you are stuck with?

It is quite simple.

Let's get the time stamp (number of seconds since the epoch) for the first day of this month:

PHP Code:
$fd_tm_ts mktime (000date ('m'), 1date ('Y')); 
(There was an error in my previous post, sorry about that, it was late)
Get the time stamp for the first day of the next month:
PHP Code:
$fd_nm_ts mktime (000date ('m')+11date ('Y')); 
Now the number of days is the difference between the two divided my the number of seconds:
PHP Code:
$nr_days = ($fd_nm_ts $fd_tm_ts)/86400
This is all there is. If you want to calculate the number of days between arbitrary dates, use the month and year directly in the mktime function instead of date('m') and date ('Y')

Check the time(), date() and mktime() functions in the PHP manual. They are quite powerful and enable you to do all date arithmetic you'll ever need for programs like you are writing now.

jlinkels
 
Old 10-20-2007, 08:21 PM   #7
waelaltaqi
Member
 
Registered: Sep 2005
Location: USA, TN
Distribution: CentOS & Ubuntu for Desktop
Posts: 454

Original Poster
Rep: Reputation: 31
PHP > Static HTML

First off, your last post has totaly solved my problem. I get correct average daily output for spam hits. Below is part of the script.
Code:
<?php 
$fd_tm_ts = mktime (0, 0, 0, date ('m'), 1, date ('Y'));       
$fd_nm_ts = mktime (0, 0, 0, date ('m')+1, 1, date ('Y'));  
$nr_days = ($fd_nm_ts - $fd_tm_ts)/86400
?>
 <tr class="second"> 
   <td> 
    <?php
            echo $TotalMailbox ;
    ?>
   </td>
   <td>	
	<?PHP
	$AVGDailyTotal = round(($AVGMonthTotal / $nr_days),1); 
	echo $AVGDailyTotal; 
	?>
   </td>
   <td>	
	<?PHP
	$AVGDailySpam = round(($AVGMonthSpam / $nr_days),1); 
	echo $AVGDailySpam; 
	?>
   </td>
      <td>	
	<?PHP
	$AVGDailyClean = round(($AVGMonthClean / $nr_days),1); 
	echo $AVGDailyClean; 
	?>
   </td>
   </td>
      <td>	
	<?PHP
	$AVGDailyVirus = round(($AVGMonthVirus / $nr_days),3); 
	echo $AVGDailyVirus; 
	?>
   </td>
</table>
i'm having another issue though. The intent of the script is to be mailed out to my clients. I'm trying to find a way to convert the output to static HTML so i could mail it out. I'm going to do my own research, but is there a way to mail the output straight from the PHP script?
 
Old 10-20-2007, 11:05 PM   #8
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
php has a mail(string to, string subject, string message) function that enables you to send email directly (assuming that you have an email service configured on the server). However you may want to consider the option to have the email as plain text.
 
Old 10-22-2007, 09:51 AM   #9
waelaltaqi
Member
 
Registered: Sep 2005
Location: USA, TN
Distribution: CentOS & Ubuntu for Desktop
Posts: 454

Original Poster
Rep: Reputation: 31
Static HTML is done... .Mailing HTML Files?

the mail function in PHP will send emails from the script but it'll will not send the output of the script itself. I found a solution by outputting the script using curl:
Code:
#curl http://localhost/myscript.php > /root/myoutput.html
Now another problem arised. I want to run a a cron job that will send the static html file every month... I tried this:

Code:
mail -s "MailsCanner Monthly reports" somebody@somedomain.com < /root/myouput.html
that sent the HTML file but in plain text. Just HTML code .. no tables no nothing ... I know that i have to define MIME type for the mail command but i couldn't find the option to do it.
I read some forms talking about modyfing MIME in sendmail config ... but my server is running POSTFIX ... i don't want to play with POSTFIX since it's highly mission critical ... Thanks in advance.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Php days calculation dylan912 Programming 1 10-19-2006 05:57 AM
Calculating age in days and month in a bash script jachba Programming 5 06-23-2006 01:37 PM
transform month number to month name in php ALInux Programming 1 11-09-2005 10:45 AM
Starting day of month, month length chrisk5527 Programming 2 03-03-2004 04:03 PM
php hit counter problems ukch Programming 9 12-03-2003 03:51 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:28 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration