PHP实现好友生日邮件提醒
我有一个想法是这样的,希望每天知道今天是我哪位好友的生日?
当然,我得首先保存我所有好友的生日信息
实现:在新浪申请免费服务器,并申请Mysql应用,然后建一张表保存好友的信息
我想,我已经完成第一步了,idcard包含着出生日期。
第二步,我们发现生日都是农历日期的,而我们常用的是公历日期
所以,我们要实现获取今天的农历日期,这个是我从网上找到的一段公历转农历,农历转公历的源代码
<?php
class Lunar
{
private $_SMDay = array(1 => 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);//定义公历月分天数
private $_LStart = 1950 ;//农历从1950年开始
private $_LMDay = array(
//差:该年的农历正月初一到该年公历1月1日的天数;1~12:农历月份天数;闰:如有闰月,记录该月平月天数
// 差 1 2 3 4 5 6 7 8 9 10 11 12 闰
array(47,29,30,30,29,30,30,29,29,30,29,30,29),
array(36,30,29,30,30,29,30,29,30,29,30,29,30),
array(6,29,30,29,30,59,29,30,30,29,30,29,30,29), //五月29 闰五月30
array(44,29,30,29,29,30,30,29,30,30,29,30,29),
array(33,30,29,30,29,29,30,29,30,30,29,30,30),
array(23,29,30,59,29,29,30,29,30,29,30,30,30,29), //三月29 闰三月30
array(42,29,30,29,30,29,29,30,29,30,29,30,30),
array(30,30,29,30,29,30,29,29,59,30,29,30,29,30), //八月30 闰八月29
array(48,30,30,30,29,30,29,29,30,29,30,29,30),
array(38,29,30,30,29,30,29,30,29,30,29,30,29),
array(27,30,29,30,29,30,59,30,29,30,29,30,29,30), //六月30 闰六月29
array(45,30,29,30,29,30,29,30,30,29,30,29,30),
array(35,29,30,29,29,30,29,30,30,29,30,30,29),
array(24,30,29,30,58,30,29,30,29,30,30,30,29,29), //四月29 闰四月29
array(43,30,29,30,29,29,30,29,30,29,30,30,30),
array(32,29,30,29,30,29,29,30,29,29,30,30,29),
array(20,30,30,59,30,29,29,30,29,29,30,30,29,30), //三月30 闰三月29
array(39,30,30,29,30,30,29,29,30,29,30,29,30),
array(29,29,30,29,30,30,29,59,30,29,30,29,30,30), //七月30 闰七月29
array(47,29,30,29,30,29,30,30,29,30,29,30,29),
array(36,30,29,29,30,29,30,30,29,30,30,29,30),
array(26,29,30,29,29,59,30,29,30,30,30,29,30,30), //五月30 闰五月29
array(45,29,30,29,29,30,29,30,29,30,30,29,30),
array(33,30,29,30,29,29,30,29,29,30,30,29,30),
array(22,30,30,29,59,29,30,29,29,30,30,29,30,30), //四月30 闰四月29
array(41,30,30,29,30,29,29,30,29,29,30,29,30),
array(30,30,30,29,30,29,30,29,59,29,30,29,30,30), //八月30 闰八月29
array(48,30,29,30,30,29,30,29,30,29,30,29,29),
array(37,30,29,30,30,29,30,30,29,30,29,30,29),
array(27,30,29,29,30,29,60,29,30,30,29,30,29,30), //六月30 闰六月30
array(46,30,29,29,30,29,30,29,30,30,29,30,30),
array(35,29,30,29,29,30,29,29,30,30,29,30,30),
array(24,30,29,30,58,30,29,29,30,29,30,30,30,29), //四月29 闰四月29
array(43,30,29,30,29,29,30,29,29,30,29,30,30),
array(32,30,29,30,30,29,29,30,29,29,59,30,30,30), //十月30 闰十月29
array(50,29,30,30,29,30,29,30,29,29,30,29,30),
array(39,29,30,30,29,30,30,29,30,29,30,29,29),
array(28,30,29,30,29,30,59,30,30,29,30,29,29,30), //六月30 闰六月29
array(47,30,29,30,29,30,29,30,30,29,30,30,29),
array(36,30,29,29,30,29,30,29,30,29,30,30,30),
array(26,29,30,29,29,59,29,30,29,30,30,30,30,30), //五月30 闰五月29
array(45,29,30,29,29,30,29,29,30,29,30,30,30),
array(34,29,30,30,29,29,30,29,29,30,29,30,30),
array(22,29,30,59,30,29,30,29,29,30,29,30,29,30), //三月30 闰三月29
array(40,30,30,30,29,30,29,30,29,29,30,29,30),
array(30,29,30,30,29,30,29,30,59,29,30,29,30,30), //八月30 闰八月29
array(49,29,30,29,30,30,29,30,29,30,30,29,29),
array(37,30,29,30,29,30,29,30,30,29,30,30,29),
array(27,30,29,29,30,58,30,30,29,30,30,29,30,29), //五月29 闰五月29
array(46,30,29,29,30,29,29,30,29,30,30,30,29),
array(35,30,30,29,29,30,29,29,30,29,30,30,29),
array(23,30,30,29,59,30,29,29,30,29,30,29,30,30), //四月30 闰四月29
array(42,30,30,29,30,29,30,29,29,30,29,30,29),
array(31,30,30,29,30,30,29,30,29,29,30,29,30),
array(21,29,59,30,30,29,30,29,30,29,30,29,30,30), //二月30 闰二月29
array(39,29,30,29,30,29,30,30,29,30,29,30,29),
array(28,30,29,30,29,30,29,59,30,30,29,30,30,30), //七月30 闰七月29
array(48,29,29,30,29,29,30,29,30,30,30,29,30),
array(37,30,29,29,30,29,29,30,29,30,30,29,30),
array(25,30,30,29,29,59,29,30,29,30,29,30,30,30), //五月30 闰五月29
array(44,30,29,30,29,30,29,29,30,29,30,29,30),
array(33,30,29,30,30,29,30,29,29,30,29,30,29),
array(22,30,29,30,59,30,29,30,29,30,29,30,29,30), //四月30 闰四月29
array(40,30,29,30,29,30,30,29,30,29,30,29,30),
array(30,29,30,29,30,29,30,29,30,59,30,29,30,30), //九月30 闰九月29
array(49,29,30,29,29,30,29,30,30,30,29,30,29),
array(38,30,29,30,29,29,30,29,30,30,29,30,30),
array(27,29,30,29,30,29,59,29,30,29,30,30,30,29), //六月29 闰六月30
array(46,29,30,29,30,29,29,30,29,30,29,30,30),
array(35,30,29,30,29,30,29,29,30,29,29,30,30),
array(24,29,30,30,59,30,29,29,30,29,30,29,30,30), //四月30 闰四月29
array(42,29,30,30,29,30,29,30,29,30,29,30,29),
array(31,30,29,30,29,30,30,29,30,29,30,29,30),
array(21,29,59,29,30,30,29,30,30,29,30,29,30,30), //二月30 闰二月29
array(40,29,30,29,29,30,29,30,30,29,30,30,29),
array(28,30,29,30,29,29,59,30,29,30,30,30,29,30), //六月30 闰六月29
array(47,30,29,30,29,29,30,29,29,30,30,30,29),
array(36,30,30,29,30,29,29,30,29,29,30,30,29),
array(25,30,30,30,29,59,29,30,29,29,30,30,29,30), //五月30 闰五月29
array(43,30,30,29,30,29,30,29,30,29,29,30,30),
array(33,29,30,29,30,30,29,30,29,30,29,30,29),
array(22,29,30,59,30,29,30,30,29,30,29,30,29,30), //三月30 闰三月29
array(41,30,29,29,30,29,30,30,29,30,30,29,30),
array(30,29,30,29,29,30,29,30,29,30,30,59,30,30), //十一月30 闰十一月29
array(49,29,30,29,29,30,29,30,29,30,30,29,30),
array(38,30,29,30,29,29,30,29,29,30,30,29,30),
array(27,30,30,29,30,29,59,29,29,30,29,30,30,29), //六月29 闰六月30
array(45,30,30,29,30,29,29,30,29,29,30,29,30),
array(34,30,30,29,30,29,30,29,30,29,29,30,29),
array(23,30,30,29,30,59,30,29,30,29,30,29,29,30), //五月30 闰五月29
array(42,30,29,30,30,29,30,29,30,30,29,30,29),
array(31,29,30,29,30,29,30,30,29,30,30,29,30),
array(21,29,59,29,30,29,30,29,30,30,29,30,30,30), //二月30 闰二月29
array(40,29,30,29,29,30,29,29,30,30,29,30,30),
array(29,30,29,30,29,29,30,58,30,29,30,30,30,29), //七月29 闰七月29
array(47,30,29,30,29,29,30,29,29,30,29,30,30),
array(36,30,29,30,29,30,29,30,29,29,30,29,30),
array(25,30,29,30,30,59,29,30,29,29,30,29,30,29), //五月29 闰五月30
array(44,29,30,30,29,30,30,29,30,29,29,30,29),
array(32,30,29,30,29,30,30,29,30,30,29,30,29),
array(22,29,30,59,29,30,29,30,30,29,30,30,29,29), //三月29 闰三月30
);
//是否闰年
private function IsLeapYear($AYear){
return ($AYear % 4 == 0) && (($AYear % 100 != 0) || ($AYear % 400 == 0));
}
//公历该月的天数(year:年份; month:月份)
private function GetSMon($year,$month)
{
if($this->IsLeapYear($year) && $month == 2)
return 29;
else
return $this->_SMDay[$month];
}
//农历名称转换
private function LYearName($year)
{
$Name = array("零","一","二","三","四","五","六","七","八","九");
for($i=0;$i<4;$i++)
for($k=0;$k<10;$k++)
if($year[$i]==$k)
$tmp.=$Name[$k];
return $tmp;
} private function LMonName($month)
{
if($month >=1 && $month <=12 )
{
$Name = array( 1=>"正","二","三","四","五","六","七","八","九","十","十一","十二");
return $Name[$month];
}
return $month;
} private function LDayName($day)
{
if($day >=1 && $day <=30 )
{
$Name = array( 1 =>
"初一","初二","初三","初四","初五","初六","初七","初八","初九","初十",
"十一","十二","十三","十四","十五","十六","十七","十八","十九","二十",
"廿一","廿二","廿三","廿四","廿五","廿六","廿七","廿八","廿九","三十"
);
return $Name[$day];
}
return $day;
} //公历转农历(Sdate:公历日期)
public function S2L($date)
{
list($year, $month, $day) = explode("-", $date);
if($year <= 1951 || $month <= 0 || $day <= 0 || $year >= 2051 ) return false;
//获取查询日期到当年1月1日的天数
$date1 = strtotime($year."-01-01");//当年1月1日
$date2 = strtotime($year."-".$month."-".$day);
$days=round(($date2-$date1)/3600/24);
$days += 1;
//获取相应年度农历数据,化成数组Larray
$Larray = $this->_LMDay[$year - $this->_LStart];
if($days <= $Larray[0])
{
$Lyear = $year - 1;
$days = $Larray[0] - $days;
$Larray = $this->_LMDay[$Lyear - $this->_LStart];
if($days < $Larray[12])
{
$Lmonth = 12;
$Lday = $Larray[12] - $days;
}
else
{
$Lmonth = 11;
$days = $days - $Larray[12];
$Lday = $Larray[11] - $days;
}
}
else
{
$Lyear = $year;
$days = $days - $Larray[0];
for($i = 1;$i <= 12;$i++)
{
if($days > $Larray[$i]) $days = $days - $Larray[$i];
else
{
if ($days > 30){
$days = $days - $Larray[13];
$Ltype = 1;
} $Lmonth = $i;
$Lday = $days;
break;
}
}
}
return mktime(0, 0, 0, $Lmonth, $Lday, $Lyear);
//$Ldate = $Lyear."-".$Lmonth."-".$Lday;
//$Ldate = $this->LYearName($Lyear)."年".$this->LMonName($Lmonth)."月".$this->LDayName($Lday);
//if($Ltype) $Ldate.="(闰)";
//return $Ldate;
}
//农历转公历(date:农历日期; type:是否闰月)
public function L2S($date,$type = 0)
{
list($year, $month, $day) = explode("-",$date);
if($year <= 1951 || $month <= 0 || $day <= 0 || $year >= 2051 ) return false;
$Larray = $this->_LMDay[$year - $this->_LStart];
if($type == 1 && count($Larray)<=12 ) return false;//要求查询闰,但查无闰月
//如果查询的农历是闰月并该年度农历数组存在闰月数据就获取
if($Larray[$month]>30 && $type == 1 && count($Larray) >=13) $day = $Larray[13] + $day;
//获取该年农历日期到公历1月1日的天数
$days = $day;
for($i=0;$i<=$month-1;$i++)
$days += $Larray[$i];
//当查询农历日期距离公历1月1日超过一年时
if($days > 366 || ($this->GetSMon($month,2)!=29 && $days>365 ))
{
$Syear = $year +1;
if($this->GetSMon($month,2)!=29)
$days-=366;
else
$days-=365;
if($days > $this->_SMDay[1])
{
$Smonth = 2;
$Sday = $days - $this->_SMDay[1];
}
else
{
$Smonth = 1;
$Sday = $days;
}
}
else
{
$Syear =$year;
for($i=1;$i<=12;$i++)
{
if($days > $this->GetSMon($Syear,$i))
$days-=$this->GetSMon($Syear,$i);
else
{
$Smonth = $i;
$Sday = $days;
break;
}
}
}
return mktime(0, 0, 0, $Smonth, $Sday, $Syear);
//$Sdate = $Syear."-".$Smonth."-".$Sday;
//return $Sdate;
}
}
?>
第二步完成!
第三步,我们循环获取身份证信息,然后截取出生日期吧,最后判断是否等于今天的农历
$mysql = new SaeMysql();
$sql = "SELECT sname,idcard FROM `wp_stu`";
$data = $mysql->getData( $sql);
$todaydate = getToday();
for( $i = 0 , $size = count ( $data ); $i < $size ; ++ $i ){
$year = substr($data[$i]['idcard'],6,4);
$month = substr($data[$i]['idcard'],10,2);
$day = substr($data[$i]['idcard'],12,2);
$date = $month."-".$day;
if(substr("$todaydate<br/>",5) == $date){
//生日
}else{
//非生日
}
} $mysql->closeDb();
哈哈,我们已经循环得到今天过生日的好友列表了
下一步,我们进行发送邮件吧
PHP发送邮件有一个插件PHPMail
需要下载两个源文件
class.phpmailer.php
class.smtp.php
$mail = new PHPMailer (); // 建立邮件发送类
$mail->Host = "smtp.qq.com"; // 企业域名
$mail->SMTPAuth = true; // 启用SMTP验证功能
$mail->Username = "857190327@qq.com"; // 用户名(请填写完整的email地址)
$mail->Password = "*********"; // 密码
$mail->Port = 25;
$mail->From = "857190327@qq.com"; // 邮件发送者email地址
$mail->FromName = "helingfeng";
$mail->AddAddress ( "857190327@qq.com", "hechao" ); // 收件人地址,可以替换成任何想要接收邮件的email信箱,格式是AddAddress("收件人email","收件人姓名")
//// $mail->AddReplyTo("", ""); //$mail->AddAttachment("/var/tmp/file.tar.gz"); // 添加附件
//$mail->IsHTML(true); // set email format to HTML //是否使用HTML格式 $mail->Subject = "生日提醒"; // 邮件标题
$mail->Body = $data[$i]['sname']."--生日<br/>"; // 邮件内容
$mail->AltBody = "helingfeng"; // 附加信息,可以省略 if (! $mail->Send ()) {
echo "邮件发送失败. <p>";
echo "错误原因: " . $mail->ErrorInfo;
exit ();
}
太好了,成功完成第三步,我已经收到邮件了
最后一个问题,我需要每天定时执行任务
PHP用这种方法吧
ignore_user_abort();
set_time_limit(0);
do{
//判断循环配置
if($configuration){
//继续判断生日
}else{
exit();
}
sleep(24*3600);//一天
}while(true);
运行一次,没问题了,就看配置文件了。
PHP实现好友生日邮件提醒的更多相关文章
- Python实现好友生日提醒
Python实现好友生日提醒
- Python-定时爬取指定城市天气(二)-邮件提醒
目录 一.概述 二.模块重新划分 三.优化定时任务 四.发送邮件 五.源代码 一.概述 上一篇文章python-定时爬取指定城市天气(一)-发送给关心的微信好友中我们讲述了怎么定时爬取城市天气,并发送 ...
- 2.jenkins配置邮件提醒
1.前言 在Jenkins的使用中邮件提醒是一个常用功能,Jenkins默认安装了Mailer Plugin插件用于实现此功能. 2.邮件服务器配置 首先在Jenkins的"系统管理&quo ...
- c#之Redis队列在邮件提醒中的应用
场景 有这样一个场景,一个邮件提醒的windows服务,获取所有开启邮件提醒的用户,循环获取这些用户的邮件,发送一条服务号消息.但问题来了,用户比较少的情况下,轮询一遍时间还能忍受,如果用户多了,那用 ...
- WordPress设置固定链接和邮件提醒遇到的问题
固定链接1.WordPress根目录下有一个.h...文件,记录文章链接类型对应的配置,要保证该文件的可写全权限:2.设置Apache2的rewrite模块启动,/etc/apache2/modle- ...
- Python 实现接口类的两种方式+邮件提醒+动态导入模块+反射(参考Django中间件源码)
实现接口类的两种方式 方式一 from abc import ABCMeta from abc import abstractmethod class BaseMessage(metaclass=AB ...
- Jenkins使用QQ邮箱构建邮件提醒服务
前置条件: 已安装好Jenkins服务端 步骤: 访问Jenkins服务端(ip+端口),登录Jenkins 点击系统管理 选择管理插件 选择可选插件 搜索Mailer Plugin 点击直接安装 点 ...
- gitlab11.5.4 配置邮件提醒
gitlab 配置邮件提醒 gitlab_rails['smtp_enable'] = true gitlab_rails['smtp_address'] = "smtp.qiye.163. ...
- EWS 邮件提醒
摘要 之前做的邮件提醒的项目,最近需要优化,由于使用了队列,但即时性不是特别好,有队列,就会出现先后的问题,最近调研了exchange 流通知的模式,所以想使用流通知模式和原先的拉取邮件的方法结合,在 ...
随机推荐
- C# 字符串驻留池
在.Net中,对于相同的字符串,.Net会将它们指向同一个地址,它们是相同的实例..Net中的字符串并不会更新,当更改一个字符串变量时,由于字符串的不可变性,.Net实际上是新创建一个字符串,而将变量 ...
- mysql 只给更新表的某个字段的授权
mysql> create view v_Procuct as select sn,name from Product; Query OK, 0 rows affected (0.01 sec) ...
- The type R is already defined 错误解决办法
今天在导入一个开源项目的时候遇到了The type R is already defined的错误,试过了删除R,clear project都还是报这个错,Google一下之后找到了解决办法在 Pro ...
- 《如何让TT T4模板输出多个文件(VS2010中)》-- access911.net 文章
<如何让TT T4模板输出多个文件(VS2010中)>-- access911.net 文章 问题: VS2010中自带的 TT 模板功能挺好用,但是如何定义其输出的目录,或者如何 ...
- ios7 UITableView底线右移
在ios7上UITableView底线右移了,我们可以通过添加代码来让它铺满整个屏幕的宽,在使用前要加上判断是否有这个方法 if ([_tableView respondsToSelector:@se ...
- hdu 3061 (最大权闭合图)
分析:城池之间有依赖关系,汇点与能获得兵力的城池连接,容量为可以获得的兵力,损耗兵力的城池与汇点连接容量为损耗的兵力,有依赖关系的城池间连边,容量为无穷大,跑网络流求出的最小割就是损耗的最小兵力,,, ...
- Java - 文件(IO流)
Java - 文件 (IO) 流的分类: > 文件流:FileInputStream | FileOutputStream | FileReader | FileWriter ...
- Gridview 多重表头 (二)
多重表头之排序 这是个有点忧桑的故事...Cynthia告诉我,研究一个问题,我们不可能有超过一天的时间... 结果好好几天过去鸟~~还没有完成... 由于不再使用Gridview自带的表头行,于是无 ...
- 从客户端(Content="<p>666</p>")中检测到有潜在危险的 Request.Form 值。
出现:从客户端(Content="<p>测试</p>")中检测到有潜在危险的 Request.Form 值. 一般是在线编辑器有HTML标签的,我是用的MV ...
- protobuf NET使用
首先,开源项目地址为: protobuf NET的GITHUB地址 下载下来后,打开项目,找到目录:Core/protobuf-net,生成一下,然后就可以在bin里得到protobuf-net.dl ...