Jump to content

convert dd-mm-yyy to timestamp format


jasonc

Recommended Posts

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

Link to comment
Share on other sites

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);

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.