|
-
Jun 7th, 2010, 05:39 PM
#1
Thread Starter
Frenzied Member
[RESOLVED] Convert Date() to Time()
Hello I have a date in a string formatted like so: "d/m/y-H:i:s" using the date function. I want to convert that specific formatted date to a UTC time. Since I need to be able to compare other dates to this one.
How do I do this?
-
Jun 7th, 2010, 06:05 PM
#2
Re: Convert Date() to Time()
If you used date() to format it, could you not simply save the pre-formatted timestamp in a separate variable?
-
Jun 7th, 2010, 06:11 PM
#3
Thread Starter
Frenzied Member
Re: Convert Date() to Time()
I thought of that at first but the issue is that a third party program is generating the date format in a text file. I need to read the text file to determine if the date is greater than the current date minus one hour.
So you see that I have no control over the generated date in the text file. I just need to retrieve the date from the text file and run a few comparisons on it.
-
Jun 7th, 2010, 06:33 PM
#4
Re: Convert Date() to Time()
if you are using PHP 5.3, you can use date_parse_from_format():
PHP Code:
<?php header("Content-type: text/plain"); date_default_timezone_set("America/Denver"); $format = "d/m/y-H:i:s"; $date = date_parse_from_format($format, date($format)); $timestamp = mktime($date['hour'], $date['minute'], $date['second'], $date['month'], $date['day'], $date['year']); print_r($date); echo $timestamp; ?>
if you are not using PHP 5.3, you'll have to parse the date and use mktime() to convert it to a Unix timestamp, and from there you can use date() to convert it however you'd like. functions like strtotime() won't work because the format your dates are in is unsupported.
-
Jun 8th, 2010, 05:39 PM
#5
Thread Starter
Frenzied Member
Re: Convert Date() to Time()
Since I'm not using PHP 5.3 I ended up just using substr's, it's not very flexible when it comes to changing the formats but it will suit my current needs.
Code:
$format = "d/m/y-H:i:s";
$date = array();
$date['day'] = substr($currDate,0,2);
$date['month'] = substr($currDate,3,2);
$date['year'] = substr($currDate,6,2);
$date['hour'] = substr($currDate,9,2);
$date['minute'] = substr($currDate,12,2);
$date['second'] = substr($currDate,15,2);
$currTimestamp = mktime($date['hour'], $date['minute'], $date['second'], $date['month'], $date['day'], $date['year']);
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|