Home > Archive > PHP Programming > November 2007 > Splitting date into chunks
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Splitting date into chunks
|
|
| Jason Carlton 2007-11-29, 4:02 am |
| 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
| |
| velhari 2007-11-29, 4:02 am |
| On Nov 29, 9:13 am, Jason Carlton <jwcarl...@gmail.com> 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
| |
| Jason Carlton 2007-11-29, 4:02 am |
| > list($year, $month, $date, $hour, $minute, $second) = split("
> ",date("Y m d H i s",timestamp));
> Hope it helps.
That's very , 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?
| |
| John Dunlop 2007-11-29, 4:02 am |
| 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
| |
| taps128 2007-11-29, 4:02 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
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.
| |
| Jason Carlton 2007-11-29, 4:02 am |
| > 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.
<snip>
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
| |
| Jason Carlton 2007-11-29, 4:02 am |
| > 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
| |
| Toby A Inkster 2007-11-29, 4:02 am |
| 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
[G 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/...itunes-sharing/
| |
| taps128 2007-11-29, 4:02 am |
| Jason Carlton wrote:
> <snip>
>
> 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.
| |
| Toby A Inkster 2007-11-29, 4:02 am |
| 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
[G 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/...itunes-sharing/
| |
| Bruno Rafael Moreira de Barros 2007-11-29, 8:00 am |
| 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.
| |
| BKDotCom 2007-11-29, 7:03 pm |
| On Nov 28, 10:13 pm, Jason Carlton <jwcarl...@gmail.com> 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');
|
|
|
|
|