/    Sign up×
Community /Pin to ProfileBookmark

Conver date and time to timestamp

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. ?

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@bionoidDec 20.2011 — 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)) &#37; 24, $matches[5], 0, $matches[2], $matches[1], $matches[3]);
echo date('Y/m/d H:i', $timestamp);
}

?>[/CODE]
Copy linkTweet thisAlerts:
@NogDogDec 21.2011 — If using PHP 5.3.0+, you could look at [url=http://www.php.net/manual/en/datetime.createfromformat.php]DateTime::createFromFormat[/url]() and [url=http://www.php.net/manual/en/datetime.gettimestamp.php]DateTime::getTimestamp[/url]().
Copy linkTweet thisAlerts:
@ZABIauthorDec 21.2011 — 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
Copy linkTweet thisAlerts:
@NogDogDec 21.2011 — 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 [url=http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_str-to-date]STR_TO_DATE[/url]() function, or else see my previous reply for converting it within PHP (assuming a reasonably current version).
×

Success!

Help @ZABI spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...