hi all, i am trying to convert the date from d/m/Y h:i A (21/12/2011 2:28 AM") to timestamp but no success. i ve also tried strtotime but it returns false not tha timestamp. any suggestion. :confused:
Printable View
hi all, i am trying to convert the date from d/m/Y h:i A (21/12/2011 2:28 AM") to timestamp but no success. i ve also tried strtotime but it returns false not tha timestamp. any suggestion. :confused:
How about a regular expression?
Code:<?php
$date = '21/12/2011 2:28 AM';
if (preg_match('|(\d+)/(\d+)/(\d+) (\d+):(\d+) (\w+)|', $date, $matches)) {
$timestamp = mktime(($matches[4] + ($matches[6] === 'PM' ? 12 : 0)) % 24, $matches[5], 0, $matches[2], $matches[1], $matches[3]);
echo date('Y/m/d H:i', $timestamp);
}
?>
If using PHP 5.3.0+, you could look at DateTime::createFromFormat() and DateTime::getTimestamp().
let me explain in detail, i have stored transaction time in a field as timestamp using PHP's time() function so that it can be displayed in different date formats. so when i want to search for records after a specific time i need to convert the time entered from a Javascript Datetime picker, and it gives me time like "21/12/2011 12:21 PM", thats what i want to convert to a timestamp and match the records in mysql query
My first suggestion would be to see if the Javascript datetime picker can be configured to give a more portable format, e.g. "2011-12-21 15:21", which you could then use directly in your query (properly escaped to avoid SQL injection, of course ;) ). If not, then either look at MySQL's STR_TO_DATE() function, or else see my previous reply for converting it within PHP (assuming a reasonably current version).