/    Sign up×
Community /Pin to ProfileBookmark

convert string-format date to a number

I have a date string in the following format:

[code]
$boardCreated=”Aug 15 2005, 08:21 PM”;
[/code]

(GMT)
FYI: This is the entry format and is unchangable because this is the format the way it is given to PHP from the message forum software I am using which I do not have access to edit directly.

I now need to change the date string into the number of seconds since the epoch. After the number of seconds since the epoch is given, I can take it from here with the math calcuations to return the number of days, hours, minutes, and seconds since the board creation by compairing it to the output of time().

Thanks in advance.

to post a comment
PHP

9 Comments(s)

Copy linkTweet thisAlerts:
@LiLcRaZyFuZzYNov 18.2005 — If the format is always the same, you could split it with a regex and transform it into timestamp format
Copy linkTweet thisAlerts:
@UltimaterauthorNov 18.2005 — That is what I want to do and ending with the number of seconds since the epoch.

The first thing to do is to split the string at " " however I am not familar with the syntax of PHP even how to do this simlpe task.

I'd imagine that the following array would be needed:
<i>
</i>array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")


I also imagine that a loop, some other calculations, and a date object or whatever it is called in PHP would be needed as well.
Copy linkTweet thisAlerts:
@LiLcRaZyFuZzYNov 18.2005 — Here i believe (and tested on the regex coach) that this pattern will work for everything in this format:
[code=php]
# word(month) 2digits(day) 4digits(year) 4digits separated by a ":" (hours:minutes)
# and last the ante/post meridiem
$pattern = "/(w)*s(d){2}s(d){4},s(d{2}:d{2})s(PM|AM)/"
[/code]
Copy linkTweet thisAlerts:
@UltimaterauthorNov 18.2005 — I was thinking more along the lines of:
<i>
</i>$pattern = "/(w)[color=blue]{3}[/color]s(d){2}s(d){4},s(d{2}:d{2})s(PM|AM)/";

But either regexp pattern will most-probably work.

However what am I supposed to do with this regular expression pattern If I am not familiar with the functionality of PHP's functions? Even if we seperate the date string into seperate varaibles, I'm still not sure how to use PHP's date object in order to get the seconds since the epoch.
Copy linkTweet thisAlerts:
@LiLcRaZyFuZzYNov 18.2005 — with the mktime function http://php.net/mktime
Copy linkTweet thisAlerts:
@UltimaterauthorNov 18.2005 — Ok awesome! Now all is left to do is execute the regular expression and get the results into an array. What is the function for that?
Copy linkTweet thisAlerts:
@LiLcRaZyFuZzYNov 18.2005 — [url=http://php.net/preg_match_all]preg_match_all()[/url]
Copy linkTweet thisAlerts:
@UltimaterauthorNov 18.2005 — Awesome thanks a bunch!
Copy linkTweet thisAlerts:
@NogDogNov 19.2005 — This seems to work:
[code=php]
$boardCreated="Aug 15 2005, 08:21 PM";
$parts = explode(" ", $boardCreated);
$time = strtotime(sprintf("%d %s %d %s%s", $parts[1], $parts[0], $parts[2], $parts[3], $parts[4]));
[/code]
×

Success!

Help @Ultimater 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.19,
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,
)...