Jump to content

Taking Away From A Date


dreampho

Recommended Posts

Hi all.

 

Okay, so I have a date saved in UNIX format, so the date looks like this: 1346689843 - {start} is just my CMS variable.

 

I am trying to first convert the date to dmY format, then remove 14 days.

 

$date = date('dmY',strtotime("{start}"));
$due_date = strtotime('-14 day',strtotime($date));
$due_date = date('dmY',$due_date);
echo $date;
echo $due_date;

 

The $date variable displays 1/1/1970 which is the first day of the UNIX I think.

$due_date displays 18/12/1969

 

So the minus 14 days is working, just not the formatting for $date. How can I get the $date variable to format the date correctly? Am I making some stupid error here?

Link to comment
Share on other sites

The $date variable displays 1/1/1970 which is the first day of the UNIX I think.
You're trying to get the date of the string '{start}'. That's not a date. You can't just assume CMS variables will be replaced in random bits of PHP code, you need to use the actual date. Also, if '{start}' looks like '1346689843', you can't call strtotime on it.

 

If you have PHP 5.3+, use the datetime library for stuff like this.

 

Link to comment
Share on other sites

Okay, I have removed strotime and now $date is formatted correctly.

 

But I dont understand how to remove the 14 days off this. Could you possibily give me an example?

 

I have read in the php manual for datetime, but its throws an error when I try and use it. Again an example would be great.

 

Thank you

Link to comment
Share on other sites

Stop using formatted dates, only format dates when you're ready to display them, it's not smart to continuously re-format and parse formatted dates throughout your code.

 

 

php > $date = time();
php > $due_date = strtotime('-14 days', $date);
php > echo date('c', $due_date);
2012-09-17T11:05:44-04:00

only use date() when you output. PHP has no way of telling if 20121001 is a date stamp or a formatted date-time string.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.