jasonc Posted January 6, 2012 Share Posted January 6, 2012 Can anyone please tell me how I convert DD-MM-YYY (text) to TIME (unix) thought I'd say that I have this line at the top of all my pages, just in case it means a different code. date_default_timezone_set('Europe/London'); //### Set the default timezone Quote Link to comment https://forums.phpfreaks.com/topic/254483-convert-dd-mm-yyy-to-timestamp-format/ Share on other sites More sharing options...
Muddy_Funster Posted January 6, 2012 Share Posted January 6, 2012 I think this is what your looking for: http://www.php.net/manual/en/datetime.createfromformat.php Quote Link to comment https://forums.phpfreaks.com/topic/254483-convert-dd-mm-yyy-to-timestamp-format/#findComment-1304839 Share on other sites More sharing options...
Adam Posted January 6, 2012 Share Posted January 6, 2012 You can use strtotime, as long as your dates contain dashes or dots but not slashes. Note: Dates in the m/d/y or d-m-y formats are disambiguated by looking at the separator between the various components: if the separator is a slash (/), then the American m/d/y is assumed; whereas if the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed. To avoid potential ambiguity, it's best to use ISO 8601 (YYYY-MM-DD) dates or DateTime::createFromFormat() when possible. I still don't trust strtotime() very much though. If you're using PHP >= 5.2 then you can take advantage of the new DateTime object, which has a range of methods that will make things a lot easier for you. Also with 5.3 you have the createFromFormat() method. For example: $date = DateTime::createFromFormat('d-m-Y', $date_str); Quote Link to comment https://forums.phpfreaks.com/topic/254483-convert-dd-mm-yyy-to-timestamp-format/#findComment-1304840 Share on other sites More sharing options...
gristoi Posted January 6, 2012 Share Posted January 6, 2012 you could explode the string and use mktime to make a timestamp $date = "06-01-2011"; // 6th jan 2012 list($day, $month, $year) = explode('-', $date); $timestamp = mktime(0, 0, 0, $month, $day, $year); will give you 1294272000 (midnight on 6th jan 2012) Quote Link to comment https://forums.phpfreaks.com/topic/254483-convert-dd-mm-yyy-to-timestamp-format/#findComment-1304846 Share on other sites More sharing options...
jasonc Posted January 6, 2012 Author Share Posted January 6, 2012 Using this method I get errors every now and then, not always.. Warning: mktime() expects parameter 5 to be long actual code i am using. $date = $_POST['makeLiveTime']; list($day, $month, $year) = explode('-', $date); $uploadTime = mktime(0, 0, 0, $month, $day, $year); echo($uploadTime); I have this placed just after the field just for testing it out. The field is not empty, it has todays date entered as default. you could explode the string and use mktime to make a timestamp $date = "06-01-2011"; // 6th jan 2012 list($day, $month, $year) = explode('-', $date); $timestamp = mktime(0, 0, 0, $month, $day, $year); will give you 1294272000 (midnight on 6th jan 2012) Quote Link to comment https://forums.phpfreaks.com/topic/254483-convert-dd-mm-yyy-to-timestamp-format/#findComment-1304873 Share on other sites More sharing options...
jasonc Posted January 6, 2012 Author Share Posted January 6, 2012 my fault I had the wrong string name that was being checked. how would I validate that the date entered was a valid date before it is converted? Quote Link to comment https://forums.phpfreaks.com/topic/254483-convert-dd-mm-yyy-to-timestamp-format/#findComment-1304890 Share on other sites More sharing options...
jasonc Posted January 6, 2012 Author Share Posted January 6, 2012 validate meaning, that the original format used is not trying to use, 31-02-2012 or other invalid dates i.e. 31-**-**** when the month only has 30 days Quote Link to comment https://forums.phpfreaks.com/topic/254483-convert-dd-mm-yyy-to-timestamp-format/#findComment-1304891 Share on other sites More sharing options...
gristoi Posted January 6, 2012 Share Posted January 6, 2012 run it through the a regular expression Quote Link to comment https://forums.phpfreaks.com/topic/254483-convert-dd-mm-yyy-to-timestamp-format/#findComment-1304897 Share on other sites More sharing options...
PFMaBiSmAd Posted January 6, 2012 Share Posted January 6, 2012 checkdate Quote Link to comment https://forums.phpfreaks.com/topic/254483-convert-dd-mm-yyy-to-timestamp-format/#findComment-1304898 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.