Jump to content

convert dd-mm-yyy to timestamp format


jasonc

Recommended Posts

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

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)

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.