javascriptphp与mysql日期计算函数
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascriptphp与mysql日期计算函数相关的知识,希望对你有一定的参考价值。
- javascript:
/** * 日期计算类 * @author [lee] <[<www.dollarphp.com>]> * @param dur 增量 如:+1day -5 year 6months * @param time 传入时间 支持格式 1、y-m-d H:i:s 2、1111111111 * @return 字符串 如:2018-01-01 00:00:00 */ function dateParse(time = false,dur){ this.getDate = function(){ var test_year = /(-)?\d+(\s)?year(s)?/.exec(dur) var test_month = /(-)?\d+(\s)?month(s)?/.exec(dur) var test_day = /(-)?\d+(\s)?day(s)?/.exec(dur) var test_hour = /(-)?\d+(\s)?hour(s)?/.exec(dur) var test_minute = /(-)?\d+(\s)?minute(s)?/.exec(dur) var test_second = /(-)?\d+(\s)?second(s)?/.exec(dur) test_year = (!test_year)?0:parseInt(test_year[0]) test_month = (!test_month)?0:parseInt(test_month[0]) test_day = (!test_day)?0:parseInt(test_day[0]) test_hour = (!test_hour)?0:parseInt(test_hour[0]) test_minute = (!test_minute)?0:parseInt(test_minute[0]) test_second = (!test_second)?0:parseInt(test_second[0]) var outtime if(time){ if(((typeof(time)==‘string‘) || (time instanceof String)) && (/^\d{4}(-\d{2}(-\d{2})?)?$/.test(time))){ outtime = new Date(time) outtime.setFullYear(parseInt(outtime.getFullYear()) + test_year) outtime.setMonth(parseInt(outtime.getMonth()) + test_month) outtime.setDate(parseInt(outtime.getDate()) + test_day) outtime.setHours(parseInt(outtime.getHours()) + test_hour) outtime.setMinutes(parseInt(outtime.getMinutes()) + test_minute) outtime.setSeconds(parseInt(outtime.getSeconds()) + test_second) var month = this.parseTime(parseInt(outtime.getUTCMonth())+1) var date = this.parseTime(parseInt(outtime.getUTCDate())) var hour = this.parseTime(parseInt(outtime.getUTCHours())) var minute = this.parseTime(parseInt(outtime.getUTCMinutes())) var second = this.parseTime(parseInt(outtime.getUTCSeconds())) }else{ outtime = new Date(time*1000) outtime.setFullYear(parseInt(outtime.getFullYear()) + test_year) outtime.setMonth(parseInt(outtime.getMonth()) + test_month) outtime.setDate(parseInt(outtime.getDate()) + test_day) outtime.setHours(parseInt(outtime.getHours()) + test_hour) outtime.setMinutes(parseInt(outtime.getMinutes()) + test_minute) outtime.setSeconds(parseInt(outtime.getSeconds()) + test_second) var month = this.parseTime(parseInt(outtime.getMonth())+1) var date = this.parseTime(parseInt(outtime.getDate())) var hour = this.parseTime(parseInt(outtime.getHours())) var minute = this.parseTime(parseInt(outtime.getMinutes())) var second = this.parseTime(parseInt(outtime.getSeconds())) } }else{ outtime = new Date() outtime.setFullYear(parseInt(outtime.getFullYear()) + test_year) outtime.setMonth(parseInt(outtime.getMonth()) + test_month) outtime.setDate(parseInt(outtime.getDate()) + test_day) outtime.setHours(parseInt(outtime.getHours()) + test_hour) outtime.setMinutes(parseInt(outtime.getMinutes()) + test_minute) outtime.setSeconds(parseInt(outtime.getSeconds()) + test_second) var month = this.parseTime(parseInt(outtime.getMonth())+1) var date = this.parseTime(parseInt(outtime.getDate())) var hour = this.parseTime(parseInt(outtime.getHours())) var minute = this.parseTime(parseInt(outtime.getMinutes())) var second = this.parseTime(parseInt(outtime.getSeconds())) } var year = outtime.getFullYear() var ret = year+‘-‘+month+‘-‘+date+‘ ‘+hour+‘:‘+minute+‘:‘+second return ret } this.parseTime = function(input){ var ret if(input >= 0 && input < 10){ ret = ‘0‘+input }else{ ret = input } return ret } } var date_parser = new dateParse(‘2018-05-08‘,‘+3hour +2year-5months‘) date = date_parser.getDate() console.log(date)
- php:
参见:date_add函数
- mysql:
参见:DATE_ADD函数
以上是关于javascriptphp与mysql日期计算函数的主要内容,如果未能解决你的问题,请参考以下文章
加密解密,MySQL单行函数,数学函数字符串日期时间,流程控制,完整详细可收藏查询SQL