473,775 Members | 2,611 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Splitting date into chunks

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
Nov 29 '07 #1
11 1807
On Nov 29, 9:13 am, Jason Carlton <jwcarl...@gmai l.comwrote:
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
Nov 29 '07 #2
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?
Nov 29 '07 #3
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
Nov 29 '07 #4
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($t imestamp,0,4);
$month=substr($ timestamp,4,2);
$day=substr($ti mestamp,6,2);
$hour=substr($t imestamp,8,2);
$minute=substr( $timestamp,10,2 );
$second=substr( $timestamp,12,2 );

I think that will solve your problem.
Nov 29 '07 #5
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
Nov 29 '07 #6
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
Nov 29 '07 #7
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/1...tunes-sharing/
Nov 29 '07 #8
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.
<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.
Nov 29 '07 #9
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/1...tunes-sharing/
Nov 29 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2077
by: somaBoy MX | last post by:
I'm building a site where I need to pull very large blocks from a database. I would like to make navigation a little more user friendly by splitting text in pages which can then be navigated. I know how to split a string in chunks, but I need to find a method to make sure the text isn't split in the middle of a word. Any ideas? Thanks,
6
1773
by: Stuart Gilbert | last post by:
I'm trying to split a string on every 2 numbers. I presumed the following would be fine, but I get nothing. var re = /{8}/; var regex = new RegExp(re); if(date.match(regex)) { var dateArray = new Array(4); dateArray = date.split(/{2}/);
6
2286
by: nwheavyw8 | last post by:
I am currently trying to write a simple PHP script that will split an uploading file up into 500kb "chunks", then read and concatenate them back together when accessed for download. I can't seem to be able to find a way to split the file purely in PHP while it is in the middle of uploading using the move_uploaded_file function. I am trying to get it to store the file like so: MySong.mp3 3mb gets uploaded to my server in this...
1
1743
by: Andy Britcliffe | last post by:
Hi I'm faced with the situation where I could have a single physical file that could contain multiplie XML documents e.g file.txt contains the following: <?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE doc SYSTEM "1.0b.dtd"> <doc transmission-date="20050715T154340Z" >
3
1589
by: La di da Limey | last post by:
Hi, I have a field in a database called "Timestamp" which has the date and time of an event, for example: "01/02/2002 09:07:59" The format is MM/DD/YYYY HH:MM:SS I want to chop the field into six seperate fields of MM, DD, YYYY, HH, MM &
5
3153
by: hecuba007 | last post by:
My apologies if this question has been asked before .. I would like to split large files into smaller chunks for uploading to php for re-assembly on the server. Is there a (relatively) simple way of doing this in javascript? If so, could someone point me to relevant documentation to read or (if really simple!) give me some coding hints? Thanks.
3
2016
by: salad | last post by:
I have an A97 application that is NOT split on a network. It is used by 15+ folks continually. It is quick and fast. I split it several years ago and had to merge it together again after the folks rebelled at the slow speed resulting from the split. I have to bite the bullet and split it now. Of course, it is much slower. In one form that opens as a continous form, the data looks like it is being repainted as it displays...you can...
4
2825
by: yogi_bear_79 | last post by:
I have a simple string (i.e. February 27, 2008) that I need to split into three parts. The month, day, and year. Splitting into a string array would work, and I could convert day and years to integers later. I've bene looking around, and everything I see seems more complicated than it should be! Help!
2
1351
by: David Jackson | last post by:
Hello, The company I'm working for has taken over a smaller company with a fairly large customer base. We want to send an email to that customer base informing them of the takeover but the mailing list is not held in a database. In fact we've been given it as a Word document. The individual email addresses are in the format: "Name <address>" e.g. Bill Gates <billg@microsoft.com>;
0
9622
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9454
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10268
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10107
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10048
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9916
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5360
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4017
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2853
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.