| | |
General Date Convertor and Calculator (Simple)
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
A simple function to change between common formats, e.g. UK, US, Unix dates and datetimes. Also allows the addition/subtraction of months, years, seconds etc etc. I have only set up a few formats in the function, but it can beeasily developed.
function mod_date($date_in,$format_in,$y,$m,$d,$h,$i,$s,$format_out){ /* Written by Alan Davies, 2009 ============================ PARAMETERS ========== $date_in: STRING date or datetime in standard uk, unix or us format. $format_in: STRING u = unix, d = uk date, a = usa date $y: Year (INTEGER), negative, positive or 0 $m: Month (INTEGER), negative, positive or 0 $d: Day (INTEGER), negative, positive or 0 $h: Hour (INTEGER), negative, positive or 0 $i: Minute (INTEGER), negative, positive or 0 $s: Second (INTEGER), negative, positive or 0 $format_out: STRING u = unix, d = uk date, a = usa date, ut = unix date + time, dt = uk date + time, at = usa date + time Example uses ============ 1. converting a date from uk -> unix -> us and viceversa mod_date('23/11/2009','d',0,0,0,0,0,0,'u') would change uk date to unix 2. adding or subtracting time to date mod_date('23/11/2009 13:00:00','d',1,0,0,-3,0,0,'ut') add one year, subtract 3 hours and place in unix format [2010-11-23 10:00:00] the function also corrects overflows (e.g. adding 13 months will actually add i year and 1 month) */ $date_in = trim($date_in); $time_element = strstr($date_in," "); if($time_element){ $datetime = explode(" ",$date_in); $date_aspect = $datetime[0]; $time_aspect = $datetime[1]; if(strstr($time_aspect,":")){ $time_parts = explode(":",$time_aspect); $dh = $time_parts[0];settype($dh,"integer"); $di = $time_parts[1];settype($dh,"integer"); if(isset($time_parts[2])){ $ds = $time_parts[2];settype($dh,"integer"); } } }else{ $date_aspect = $date_in; } if(!isset($dh) || $dh == "" || !is_int($dh))$dh = 0; if(!isset($di) || $di == "" || !is_int($di))$di = 0; if(!isset($ds) || $ds == "" || !is_int($ds))$ds = 0; if(strstr($date_aspect,"/"))$date_parts = explode("/",$date_aspect); if(strstr($date_aspect,"-"))$date_parts = explode("-",$date_aspect); switch($format_in){ case 'd': $dd = $date_parts[0]; $dm = $date_parts[1]; $dy = $date_parts[2]; break; case 'u': $dd = $date_parts[2];$dm = $date_parts[1];$dy = $date_parts[0]; break; case 'a': $dd = $date_parts[1];$dm = $date_parts[0];$dy = $date_parts[2]; break; default: $error_msg = "You must provide a proper format for the input date: chose from 'u' (unix date/time), 'd' (UK date/time), 'a' (US date/time)."; return $error_msg; break; } $date = mktime($dh + $h,$di + $i,$ds + $s,$dm + $m,$dd + $d, $dy + $y); switch($format_out){ case 'd': $my_date = date('d/m/Y',$date); break; case 'u': $my_date = date('Y-m-d',$date); break; case 'a': $my_date = date('m/d/Y',$date); break; case 'dt': $my_date = date('d/m/Y H:i:s',$date); break; case 'ut': $my_date = date('Y-m-d H:i:s',$date); break; case 'at': $my_date = date('m/d/Y H:i:s',$date); break; default: $error_msg = "You must provide a proper format for output: chose from 'u' (unix date), 'd' (UK date), 'a' (US date), 'ut' (unix datetime), 'dt' (UK datetime), 'at' (US datetime)."; return $error_msg; break; } return $my_date; }
Similar Threads
- C++ simple calculator (C++)
- Simple Calculator (C)
- Code Snippet: A Simple Calculator (C)
| Thread Tools | Search this Thread |
.htaccess ajax apache api array beginner binary broken buttons cakephp checkbox class cms code cron curl database date directory display download dynamic ebooks echo email error file files folder form forms function functions google href htaccess html image include insert integration ip java javascript joomla limit link login loop mail mediawiki menu mlm mod_rewrite multiple mysql number oop paypal pdf php phpincludeissue phpmyadmin problem query radio random recursion regex remote script search server sessions sms soap source sp space speed sql subdomain syntax system table tag tutorial update upload url validation validator variable vbulletin video web websphere white xml youtube



