strtotime query

General discussions related to php

Moderators: macek, egami, gesf

shasocastris
New php-forum User
New php-forum User
Posts: 5
Joined: Wed Sep 26, 2012 7:56 am

strtotime query

Postby shasocastris » Wed Sep 26, 2012 8:09 am

All,

So, I'm totally new at PHP and Drupal, so if this is a newbish question, I completely acknowledge the fact. However, I do need some help, so anything would be appreciated.

Currently, I'm working on this snippet of code:

preg_match('/<date>.+?<\/date>/',$article_contents,$matches);
$article_date = $matches[0];
$article_timestamp =date('Y-m-d', strtotime($matches[0]));
drupal_set_message(t("Article timestamp: ".$article_timestamp));
drupal_set_message(t("date content: ".$matches[0]));

The general code takes an .xml file and searches for this: <date>2012-09-21</date>
This the preg_match statement.

What I'm trying to do with drupal is have the article that's being posted to a drupal run website is
$node->created = $article_timestamp;

But it outputs: 1969-12-31

This is -1 in unix time, the moment before the Unix epoch began. I set $article_timestamp to -1 just to check.

Why is it doing this? I'm trying to have the article authored on the date it was posted, but I can't figure out how. Another variant is setting $article_timestamp = strtotime($matches[0]) but that sets the date to the current day.

What is weird is that if I do this:
$fixed_date="2012-02-03";
$article_timestamp = strtotime($fixed_date);
drupal_set_message(t("from fixed to variable:".$article_timestamp));

It works properly. If someone could help me out, I'd really appreciate it. And if you need any more info, I'll be glad to give it.

Cheers!

User avatar
egami
php-forum GURU
php-forum GURU
Posts: 2196
Joined: Wed Oct 06, 2010 11:19 am
Location: Happy Valley, UT

Re: strtotime query

Postby egami » Wed Sep 26, 2012 2:25 pm

Code: Select all

if ($article_date != NULL || $article_date != ''
{
  echo 
'<h1>ARTICLE DATE: '.$article_date.'</h1><br />';
} else { 
  echo 
"Pregmatch failed.";
}
 

shasocastris
New php-forum User
New php-forum User
Posts: 5
Joined: Wed Sep 26, 2012 7:56 am

Re: strtotime query

Postby shasocastris » Wed Sep 26, 2012 7:11 pm

So, I guess I should have been more specific.

It's not $article_date that's the problem. It outputs what it should. The problem seems to be in the strtotime(matches[0]) part of the code, wherever it shows up. Either I do this:
$article_timestamp =date('Y-m-d', strtotime($matches[0]));
and I get the day 1969-12-31

or I use $article_timestamp = strtotime($matches[0])
and I get posted by drupal the time()

Basically what I'm after is if I have an article with a date in the format 'Y-m-d', I want the article to be posted with that day, not a day before the unix epoch began and not the current time. What was given is useful to know, but I already knew that $article_date != NULL.

Cheers!

User avatar
egami
php-forum GURU
php-forum GURU
Posts: 2196
Joined: Wed Oct 06, 2010 11:19 am
Location: Happy Valley, UT

Re: strtotime query

Postby egami » Thu Sep 27, 2012 7:28 am

And you're sure that $matches['0'] is displaying the proper data?

shasocastris
New php-forum User
New php-forum User
Posts: 5
Joined: Wed Sep 26, 2012 7:56 am

Re: strtotime query

Postby shasocastris » Thu Sep 27, 2012 8:50 am

This line does output the correct data, which is why I'm confused about what strtotime is doing.
drupal_set_message(t("date content: ".$matches[0])); //outputs the date: 2012-09-22 for example

Cheers!

User avatar
egami
php-forum GURU
php-forum GURU
Posts: 2196
Joined: Wed Oct 06, 2010 11:19 am
Location: Happy Valley, UT

Re: strtotime query

Postby egami » Thu Sep 27, 2012 10:31 am

Ok.

First things first.
Your "$matches['0']" includes the XML tags. strtotime doesn't know how to deal with them.
You may find it a bit easier to match against

'/[0-9]{4}-[0-9]{2}-[0-9]{2}/'


I think you'll find this makes the difference.

To make it even more legit.. do a double preg_match, this way you get just the date tag, and then extract the number.


ie..

Code: Select all


preg_match
('/<date>.+?<\/date>/',$article_contents,$matches);
preg_match('/[0-9]{4}-[0-9]{2}-[0-9]{2}/',$matches['0'],$matches);

$article_date $matches[0];
$article_timestamp =date('Y-m-d'strtotime($matches[0]));
drupal_set_message(t("Article timestamp: ".$article_timestamp));
drupal_set_message(t("date content: ".$matches[0]));
 

User avatar
egami
php-forum GURU
php-forum GURU
Posts: 2196
Joined: Wed Oct 06, 2010 11:19 am
Location: Happy Valley, UT

Re: strtotime query

Postby egami » Thu Sep 27, 2012 10:33 am

But in the end..
why would you need to do a strtotime conversion, when you already have the date.

ie.
preg_match('/<date>.+?<\/date>/',$article_contents,$matches);
preg_match('/[0-9]{4}-[0-9]{2}-[0-9]{2}/',$matches['0'],$matches);

$article_timestamp = $matches['0'];

drupal_set_message(t("Article timestamp: ".$article_timestamp));
drupal_set_message(t("date content: ".$matches[0]));

shasocastris
New php-forum User
New php-forum User
Posts: 5
Joined: Wed Sep 26, 2012 7:56 am

Re: strtotime query

Postby shasocastris » Thu Sep 27, 2012 11:22 am

Oh, excellent. I would never have thought to do a double pregmatch. That solved the problem, and I see about the xml tags still being around. That would confuse the program. Thanks a lot!

Cheers!

User avatar
egami
php-forum GURU
php-forum GURU
Posts: 2196
Joined: Wed Oct 06, 2010 11:19 am
Location: Happy Valley, UT

Re: strtotime query

Postby egami » Thu Sep 27, 2012 11:42 am

So, you weren't sure that $matches['0'] output the right data, you were just guessing. :)

shasocastris
New php-forum User
New php-forum User
Posts: 5
Joined: Wed Sep 26, 2012 7:56 am

Re: strtotime query

Postby shasocastris » Thu Sep 27, 2012 12:49 pm

Actually, that's an interesting question, and why I was so frustrated. Before the change you gave me, this line:
drupal_set_message(t("date content: ".$matches[0]));

did in fact output the date formated like so: 2012-09-24

To go back a bit, this is the line in the xml file that I'm retrieving with the first pregmatch:
<date>2012-09-24</date>

With the preg match line being:
preg_match('/<date>.+?<\/date>/',$article_contents,$matches);

and the rest of the original code being:

$article_date = $matches[0];
$article_timestamp =strtotime($matches[0]); //one of the two variants I used, see the first post
drupal_set_message(t("Article timestamp: ".$article_timestamp)); //outputs time() without the extra pregmatch
drupal_set_message(t("date content: ".$matches[0])); //outputs the date formatted as 'Y-m-d'

So it seems there is still something to work through here. Why did $article_date output the date? Did something somewhere get rid of the xml tags automatically?

Cheers!


Return to “PHP General”

Who is online

Users browsing this forum: Bing [Bot] and 1 guest