Jump to content

How to set date created and published


OllieMackJames
 Share

Recommended Posts

I have a site that exists since 2003, but has been on different platforms, html, modx and then processwire.

And then on processwire we also migrated the site to a whole new design, which meant we started from scratch and imported some stuff. As a result my created and published dates are all over the place and not showing the history of the site.

OK what is my question?

How can I (re-)set the published and created dates for individual pages.

Par example the homepage is created in 2003, but since the redesign of the site in 2015 proceswire now says the homewpage was created and published in 2015, but that is not true of course.

Hence I want to set the created and published date back to the start of the site and then show the last modification to the page via datemodified.

Anybody know how and where to set the published and created dates?

Thanks.

Link to comment
Share on other sites

Do you mean in the page editor? I'll look if there are modules for this because I'm not sure.

But you can also do this via the api:  

$home = $pages->get('/');
echo date("d M Y", $home->published) . PHP_EOL; // 21 Feb 2017
echo date("d M Y", $home->created)  . PHP_EOL; // 21 Feb 2017
$home->published = strtotime("February 21, 2016");
$home->created = strtotime("February 21, 2016");
$home->save();
echo date("d M Y", $home->published) . PHP_EOL; // 21 Feb 2016
echo date("d M Y", $home->created)  . PHP_EOL; // 21 Feb 2016

Edit:

My bad. The changes doesn't seem to persist.

Turns out, published, created, and modified are not "fields" per se, but rather are properties built-in to the page. This might be a good candidate for a quick module for those who need to edit the values. 

  • Like 1
Link to comment
Share on other sites

Its not straightforward and sometimes frowned upon >:(:). Have a look at this thread.

And this one:

 

And this one for changing published date:

 

OK, if you've decided to carry on, first, some quick by the way technical tips.

If unsure whether something is a runtime or DB property, I check the API: http://processwire.com/api/ref/page/

For $page->created, it says: 

Quote

Unix timestamp of when the page was created.

That gives me an idea that when setting, I should probably set in the same format (Unix timestamp). If unsure, I can check the code or try to pass it a normal formatted date. You never know, ProcessWire can convert it behind the scenes.

I sometimes check the database table of the field. This case, the table is pages. This helps me to check out the schema and if the property is runtime or saved to DB. 

page-db-table.thumb.png.9adfc7c735ddba941f769c12f3f7b361.png

There they are. They are columns in the pages table. If need to, I also check the table structure, but don't need to in this case.

OK, to the code.

 

// CURRENT DATES
// timestamps
echo 'Created Date: ' .  $page->created . '<br>';
echo 'Published Date: ' .  $page->published . '<br>';
echo 'Modified Date: ' . $page->modified . '<br><hr>';
// formatted date
echo 'Created Date: ' .  date('D d M Y H:m', $page->created) . '<br>';
echo 'Published Date: ' .  date('D d M Y H:m', $page->published) . '<br>';
echo 'Modified Date: ' .  date('D d M Y H:m', $page->modified) . '<br>';
// MODIFY CREATED DATE
$page->created = 1425898824;// Monday, 9 March 2015 11:00:24
$page->published = 1426762824;// Thursday, 19 March 2015 11:00:24// will not work; @see links to threads above
$page->of(false);
#$page->save();// @NOTE: WILL NOT WORK; you need below
$page->save(array('quiet' => true));// @NOTE: ONLY WORKS FOR CREATED
// MODIFIED CREATED DATE
echo '<hr>';
// timestamps
echo 'Created Date: ' .  $page->created . '<br>';
echo 'Published Date: ' .  $page->published . '<br>';
echo 'Modified Date: ' . $page->modified . '<br><hr>';
// formatted dates
echo 'Created Date: ' .  date('D d M Y H:m', $page->created) . '<br>';
echo 'Published Date: ' .  date('D d M Y H:m', $page->published) . '<br>';
echo 'Modified Date: ' .  date('D d M Y H:m', $page->modified) . '<br>';

 

  • Like 6
Link to comment
Share on other sites

This will take care of changing the published date:

    $query = $database->prepare("UPDATE pages SET published=:pub_date WHERE id=:page_id");
    $query->execute([
        ':pub_date' => $pubDate,
        ':page_id' => $pageId
    ]);

 

  • Like 5
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...