Splitting date into chunks
Splitting date into chunks
am 29.11.2007 05:13:58 von Jason Carlton
I'm sure this is an easy one, but I can't seem to find it! I have the
date and time as:
# Nov 28, 2007, 11:11:14pm
$timestamp = 20071128231114;
In Perl, I would split this up as:
my ($year, $month, $day, $hr, $min, $sec) = $timestamp =~ /(\d{4})(\d
\d)(\d\d)(\d\d)(\d\d)(\d\d)/;
How do I do the same thing in PHP? I know that I can use substr, of
course, but there has to be a better way that I'm overlooking.
TIA,
Jason
Re: Splitting date into chunks
am 29.11.2007 06:56:09 von velhari
On Nov 29, 9:13 am, Jason Carlton wrote:
> I'm sure this is an easy one, but I can't seem to find it! I have the
> date and time as:
>
> # Nov 28, 2007, 11:11:14pm
> $timestamp = 20071128231114;
>
> In Perl, I would split this up as:
>
> my ($year, $month, $day, $hr, $min, $sec) = $timestamp =~ /(\d{4})(\d
> \d)(\d\d)(\d\d)(\d\d)(\d\d)/;
>
> How do I do the same thing in PHP? I know that I can use substr, of
> course, but there has to be a better way that I'm overlooking.
>
> TIA,
>
> Jason
Jason,
list($year, $month, $date, $hour, $minute, $second) = split("
",date("Y m d H i s",timestamp));
Hope it helps.
Thanks,
Velhari
Re: Splitting date into chunks
am 29.11.2007 07:56:49 von Jason Carlton
> list($year, $month, $date, $hour, $minute, $second) = split("
> ",date("Y m d H i s",timestamp));
> Hope it helps.
That's very cool, but it's not working quite right. I've tried several
variations, and keep getting the wrong thing.
If I type in:
$timestamp = 20050714010317;
echo date('Y m d H i s', $timestamp);
Then it returns:
2038 01 18 22 13 44
Any idea where that's coming from?
Re: Splitting date into chunks
am 29.11.2007 09:47:15 von John Dunlop
Jason Carlton:
> I'm sure this is an easy one, but I can't seem to find it! I have the
> date and time as:
>
> # Nov 28, 2007, 11:11:14pm
> $timestamp = 20071128231114;
>
> In Perl, I would split this up as:
>
> my ($year, $month, $day, $hr, $min, $sec) = $timestamp =~ /(\d{4})(\d
> \d)(\d\d)(\d\d)(\d\d)(\d\d)/;
The preg_* functions take Perl-compatible regular expressions.
http://www.php.net/manual/en/ref.pcre.php
--
Jock
Re: Splitting date into chunks
am 29.11.2007 10:00:24 von taps128
Jason Carlton wrote:
> I'm sure this is an easy one, but I can't seem to find it! I have the
> date and time as:
>
> # Nov 28, 2007, 11:11:14pm
> $timestamp = 20071128231114;
>
> In Perl, I would split this up as:
>
> my ($year, $month, $day, $hr, $min, $sec) = $timestamp =~ /(\d{4})(\d
> \d)(\d\d)(\d\d)(\d\d)(\d\d)/;
>
> How do I do the same thing in PHP? I know that I can use substr, of
> course, but there has to be a better way that I'm overlooking.
>
> TIA,
>
> Jason
I think you don't have a timestamp in the true sense, I think your
$timestamp variables shows an unformated date '2007-11-28 23:11:14'.
Which is what you looked for in your regular expression in perl, i think.
In php you can use:
$year=substr($timestamp,0,4);
$month=substr($timestamp,4,2);
$day=substr($timestamp,6,2);
$hour=substr($timestamp,8,2);
$minute=substr($timestamp,10,2);
$second=substr($timestamp,12,2);
I think that will solve your problem.
Re: Splitting date into chunks
am 29.11.2007 10:30:04 von Jason Carlton
> I think you don't have a timestamp in the true sense, I think your
> $timestamp variables shows an unformated date '2007-11-28 23:11:14'.
>
> Which is what you looked for in your regular expression in perl, i think.
I guess it's usually called a "datetime" instead of a "timestamp". I
didn't know that until I started researching it tonight, though.
You're correct, the format I have doesn't have any delimiters, it's
just a series of 14 numbers. I have been using substr, like you
suggested, but since the format I used in Perl was a lot faster than
the Perl version of substr, I was hoping that there might be a faster
option in PHP, too.
Using substr seems to be pretty fast at the moment, though, so maybe
it's not too bad. PHP seems to process a bit faster than Perl did,
probably because it doesn't need the external DBI module.
- Jason
Re: Splitting date into chunks
am 29.11.2007 10:32:10 von Jason Carlton
> The preg_* functions take Perl-compatible regular expressions.
>
> http://www.php.net/manual/en/ref.pcre.php
>
> --
> Jock
Jock, I did NOT realize this! Wow, you've just opened up a whole new
world for me. I've been programming in Perl for about 12 years and
only recently started using PHP, so these expressions are going to
make a world of difference.
Just curious, are these expressions slower than innate PHP
expressions? The whole reason I started using PHP in the first place
was for a little extra speed while utilizing databases.
- Jason
Re: Splitting date into chunks
am 29.11.2007 10:33:10 von Toby A Inkster
velhari wrote:
> list($year, $month, $date, $hour, $minute, $second) = split(" ",date("Y
> m d H i s",timestamp));
Argh no! Take a look at his timestamp -- it's not a Unix timestamp, but an
ISO date with the punctuation removed.
Try:
preg_match('/(\d{4})(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)/', $timestamp, $m);
list($dummy, $year, $month, $date, $hour, $minute, $second) = $m;
unset($dummy, $m);
--
Toby A Inkster BSc (Hons) ARCS
[Geek of HTML/SQL/Perl/PHP/Python/Apache/Linux]
[OS: Linux 2.6.17.14-mm-desktop-9mdvsmp, up 4 days, 16:18.]
[Now Playing: Semisonic - Sunshine and Chocolate]
Sharing Music with Apple iTunes
http://tobyinkster.co.uk/blog/2007/11/28/itunes-sharing/
Re: Splitting date into chunks
am 29.11.2007 10:37:44 von Toby A Inkster
Jason Carlton wrote:
> Just curious, are these expressions slower than innate PHP expressions?
The preg_* functions are actually faster than the ereg_* functions. In
fact, the ereg_* functions are being moved out of the PHP core soon and
into an extension -- that may have even happened already.
--
Toby A Inkster BSc (Hons) ARCS
[Geek of HTML/SQL/Perl/PHP/Python/Apache/Linux]
[OS: Linux 2.6.17.14-mm-desktop-9mdvsmp, up 4 days, 16:26.]
[Now Playing: Crowded House - Better Be Home Soon]
Sharing Music with Apple iTunes
http://tobyinkster.co.uk/blog/2007/11/28/itunes-sharing/
Re: Splitting date into chunks
am 29.11.2007 10:50:10 von taps128
Jason Carlton wrote:
>> I think you don't have a timestamp in the true sense, I think your
>> $timestamp variables shows an unformated date '2007-11-28 23:11:14'.
>>
>> Which is what you looked for in your regular expression in perl, i think.
>
>
> I guess it's usually called a "datetime" instead of a "timestamp". I
> didn't know that until I started researching it tonight, though.
>
> You're correct, the format I have doesn't have any delimiters, it's
> just a series of 14 numbers. I have been using substr, like you
> suggested, but since the format I used in Perl was a lot faster than
> the Perl version of substr, I was hoping that there might be a faster
> option in PHP, too.
>
> Using substr seems to be pretty fast at the moment, though, so maybe
> it's not too bad. PHP seems to process a bit faster than Perl did,
> probably because it doesn't need the external DBI module.
>
> - Jason
Well, I don't know about speed. I haven't programmed in perl before, but
string functions in php are pretty fast, and easy to use. Regex is not
my strong suit so I tend to favor those when dealing strings.
Re: Splitting date into chunks
am 29.11.2007 12:56:12 von brunormbarros
If I type in:
$timestamp = 20050714010317;
echo date('Y m d H i s', $timestamp);
Then it returns:
2038 01 18 22 13 44
---
Then
$timestamp = 20050714010317;
$date = date('Y m d H i s', $timestamp);
$splitted_date = explode(' ',$date); // This is now an array
containing all the date parts.
echo $splitted_date[0]; // Prints the Year part of the date.
Re: Splitting date into chunks
am 29.11.2007 19:29:27 von BKDotCom
On Nov 28, 10:13 pm, Jason Carlton wrote:
> I'm sure this is an easy one, but I can't seem to find it! I have the
> date and time as:
>
> # Nov 28, 2007, 11:11:14pm
> $timestamp = 20071128231114;
>
> In Perl, I would split this up as:
>
> my ($year, $month, $day, $hr, $min, $sec) = $timestamp =~ /(\d{4})(\d
> \d)(\d\d)(\d\d)(\d\d)(\d\d)/;
>
> How do I do the same thing in PHP? I know that I can use substr, of
> course, but there has to be a better way that I'm overlooking.
>
> TIA,
>
> Jason
I'm a bit slow, but I'd accomplish it thusly:
$datetime = 20071128231114;
list($year, $month, $date, $hour, $minute, $second) =
sscanf($datetime,'%4s%2s%2s%2s%2s%2s');