XpertWorlock Posted November 30, 2008 Share Posted November 30, 2008 Trying to build a function that changes a gregorian time to unix time. This function should work, but I am thinking the problem is with the 'GregoriantoJD' function not being able to process $month,$day,$year. Anyways, if someone could make this work function gtu($gregoriantime) { $month = date('m', strtotime($gregoriantime)); $day = date('d', strtotime($gregoriantime)); $year = date('Y', strtotime($gregoriantime)); $jd = GregorianToJD($month,$day,$year); $unixtime = jdtounix($jd); return $unixtime; }; $test = gtu(10/20/2008); echo $test; Outputs nothing Quote Link to comment https://forums.phpfreaks.com/topic/134887-solved-gregorian-to-unix/ Share on other sites More sharing options...
Guardian-Mage Posted November 30, 2008 Share Posted November 30, 2008 I am no expert with PHP time, but shouldn't it be: $test = gtu("10/20/2008"); echo $test; See the quotes? EDIT: Just tried it, and the quotes fixed it Quote Link to comment https://forums.phpfreaks.com/topic/134887-solved-gregorian-to-unix/#findComment-702375 Share on other sites More sharing options...
XpertWorlock Posted November 30, 2008 Author Share Posted November 30, 2008 Yeah I tried it too, and it works perfectly. Thanks, always the simplest thing that screws it up Quote Link to comment https://forums.phpfreaks.com/topic/134887-solved-gregorian-to-unix/#findComment-702384 Share on other sites More sharing options...
DarkWater Posted November 30, 2008 Share Posted November 30, 2008 Umm...strtotime() would already give you a Unix time. Unless I'm completely misunderstanding your goal. Quote Link to comment https://forums.phpfreaks.com/topic/134887-solved-gregorian-to-unix/#findComment-702390 Share on other sites More sharing options...
XpertWorlock Posted November 30, 2008 Author Share Posted November 30, 2008 The goal is to get gregorian to unix time as clean as possible, if you have a better way, feel free to say it Quote Link to comment https://forums.phpfreaks.com/topic/134887-solved-gregorian-to-unix/#findComment-702393 Share on other sites More sharing options...
DarkWater Posted November 30, 2008 Share Posted November 30, 2008 Yeah, wouldn't this just give you the exact same result? echo strtotime('10/20/2008'); Quote Link to comment https://forums.phpfreaks.com/topic/134887-solved-gregorian-to-unix/#findComment-702398 Share on other sites More sharing options...
XpertWorlock Posted November 30, 2008 Author Share Posted November 30, 2008 It does in a way, but your way makes it at 5am (Why I don't know?) yours make this 1224478800 10/20/2008 5:00 GMT mine makes this 1224460800 10/20/2008 0:00 GMT Could this have something to do with server settings or something? Doesn't strtotime have something to do with the client or whatever. I'm from Eastern Canada, so my time is -5 GMT Quote Link to comment https://forums.phpfreaks.com/topic/134887-solved-gregorian-to-unix/#findComment-702401 Share on other sites More sharing options...
XpertWorlock Posted November 30, 2008 Author Share Posted November 30, 2008 http://www.csgnetwork.com/unixds2timecalc.html Unix to Gregorian, to test if you need to. Quote Link to comment https://forums.phpfreaks.com/topic/134887-solved-gregorian-to-unix/#findComment-702404 Share on other sites More sharing options...
DarkWater Posted November 30, 2008 Share Posted November 30, 2008 You could always use date_default_timezone_set('UTC') if you want GMT time. Quote Link to comment https://forums.phpfreaks.com/topic/134887-solved-gregorian-to-unix/#findComment-702405 Share on other sites More sharing options...
XpertWorlock Posted November 30, 2008 Author Share Posted November 30, 2008 No I don't think GMT is really needed in this. /*Converts Gregorian month/day/year to unix time*/ function gtu($gregoriantime) { $month = date('m', strtotime($gregoriantime)); $day = date('d', strtotime($gregoriantime)); $year = date('Y', strtotime($gregoriantime)); $jd = GregorianToJD($month,$day,$year); $unixtime = jdtounix($jd); return $unixtime; }; /*Converts unix time to gregorian*/ function utg($unixtime) { $julius=unixtojd($unixtime); $Gregorian=jdtogregorian("$julius"); return $Gregorian; }; $starttime = "10/20/2008"; echo $starttime."= the date we started with<br>"; $test = gtu("$starttime"); echo $test."= This is the outcome of gregorian to unix<br>"; $test = utg("$test"); echo $test."= This is the outcome of unix to gregorian<br>"; This outputs the unix time of = "1224460800" Which is 10/20/2008 0:00 GMT when I change it back to Gregorian it comes up 10/19/2008. I'm digging myself in deeper, but just so everyone knows, there is a point to this. So what's the matter with this : function utg($unixtime) { $julius=unixtojd($unixtime); $Gregorian=jdtogregorian("$julius"); return $Gregorian; }; that it comes up the wrong day? Even this : date('m/d/Y', 1224460800); Which is 10/20/2008, comes up 10/19/2008 Quote Link to comment https://forums.phpfreaks.com/topic/134887-solved-gregorian-to-unix/#findComment-702414 Share on other sites More sharing options...
DarkWater Posted November 30, 2008 Share Posted November 30, 2008 And what does: date('m/d/Y', 1224468800); Yield? My guess is that it's your server, and strtotime() and date() would be the easiest and most efficient way to do this. Quote Link to comment https://forums.phpfreaks.com/topic/134887-solved-gregorian-to-unix/#findComment-702437 Share on other sites More sharing options...
XpertWorlock Posted November 30, 2008 Author Share Posted November 30, 2008 It's all timezone screw ups. date_default_timezone_set('GMT') Thanks Dark Water, since you cleaned up a bunch of unneeded coding too Quote Link to comment https://forums.phpfreaks.com/topic/134887-solved-gregorian-to-unix/#findComment-702440 Share on other sites More sharing options...
DarkWater Posted November 30, 2008 Share Posted November 30, 2008 No problem. You can also set the timezone in your php.ini if you don't want to have to include that line in every page. If you do decide to do it like that though, I'd suggest throwing it in a general header file that you just include. Quote Link to comment https://forums.phpfreaks.com/topic/134887-solved-gregorian-to-unix/#findComment-702442 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.