ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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?
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.
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195
Rep:
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:
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.
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:
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.
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195
Rep:
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 (0, 0, 0, date ('m'), 1, date ('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 (0, 0, 0, date ('m')+1, 1, date ('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.
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?
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.
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:
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.