Advertisement:

Author Topic: How to show time of any posted item  (Read 9185 times)

mkmobi

  • Full Member
  • ***
  • Posts: 119
Re: How to show time of any posted item
« Reply #15 on: July 31, 2013, 12:46:35 am »
not working
i got a big error after updating function.php  ???
 

TheDeadLives

  • Sr. Member
  • ****
  • Posts: 279
Re: How to show time of any posted item
« Reply #16 on: July 31, 2013, 01:16:32 am »
you must not put <?php , you must put the code in the already created php in function without <?php><?>

frosticek

  • Hero Member
  • *****
  • Posts: 3948
Re: How to show time of any posted item
« Reply #17 on: July 31, 2013, 01:47:11 am »
I think he wanted to say, you cannot place that code between <?php ?>, so if you have something like this:
<?php
code...
code...
code...
?>

then add before that code tag ?> and after that code tag <?php

you must not put <?php , you must put the code in the already created php in function without <?php><?>

TheDeadLives

  • Sr. Member
  • ****
  • Posts: 279
Re: How to show time of any posted item
« Reply #18 on: July 31, 2013, 03:43:04 am »
I think he wanted to say, you cannot place that code between <?php ?>, so if you have something like this:
<?php
code...
code...
code...
?>

then add before that code tag ?> and after that code tag <?php

you must not put <?php , you must put the code in the already created php in function without <?php><?>

Yes, that was what i was trying to explain

serjuc11111

  • Hero Member
  • *****
  • Posts: 814
Re: How to show time of any posted item
« Reply #19 on: October 02, 2013, 12:04:48 pm »
I have one code. If the item was posted today, it will show today. If it was posted yesterday it will show yesterday, etc. It is working in osclass 3.1.2

Code: [Select]
<?php

$date 
osc_item_field("dt_pub_date");
$yesterday 172800;
$today 86400;
$time strtotime($date);
$time time()-$time;

if (
$today $time) {echo '<b>Today</b>';}
else if (
$yesterday $time && $yesterday $today) {echo '<b>Yesterday</b>';}
else if (
$time$yesterday) {echo osc_format_date(osc_item_pub_date());}

?>

this code needs one more conditions....
day and at 24:00...if ad was posted yesterday at 23:00 and current date/time is "today" 01:00 am this will show ad was posted today..what is wrong....was posted yesterday...this conditions is no need if ($today > $time)

dev101

  • Osclass Hero
  • Hero Member
  • *
  • Posts: 2155
  • osclass.work
Re: How to show time of any posted item
« Reply #20 on: October 02, 2013, 03:39:51 pm »
Here is the much better code I personally wrote! It is actually my first php solution that does something usefull. :)
Of course, thanks to TheDeadLives for the original idea!

Reference points are item's relative publishing Date&Time, not absolute ones, so I dropped ref points like 'today' and 'yesterday' in my code simply because I couldn't figure how to subtract absolute dates, instead of time. After all, relative time is more informative.

Code: [Select]
<?php
$pubdate osc_item_field("dt_pub_date");
$pubdateformat strtotime($pubdate);
$now time();
$diff $now $pubdateformat;
$diffhours floor($diff/3600);
$diffminutes floor($diff/60);
$zero 0;
$oneminute 1;
$sixtyminutes 60;
$onehour 1;
$oneday 24;
$twodays 48;
$threedays 72;
$fourdays 96;
$fivedays 120;
$sixdays 144;
$sevendays 168;
$eightdays 192;
$ninedays 216;
$tendays 240;
$elevendays 264;
$twelvedays 288;
$thirteendays 312;
$fourteendays 336;
$fifteendays 360;

if ($diffminutes == $zero) {echo 'just posted!' ;}
else if ($diffminutes == $oneminute) {echo '1 minute ago' ;}
else if ($diffminutes $oneminute && $diffminutes $sixtyminutes) {echo $diffminutes ' minutes ago' ;}
else if ($diffhours == $onehour) {echo '1 hour ago';}
else if ($diffhours $onehour && $diffhours $oneday) {echo $diffhours ' hours ago';}
else if ($diffhours >= $oneday && $diffhours $twodays) {echo '1 day ago';}
else if ($diffhours >= $twodays && $diffhours $threedays) {echo '2 days ago';}
else if ($diffhours >= $threedays && $diffhours $fourdays) {echo '3 days ago';}
else if ($diffhours >= $fourdays && $diffhours $fivedays) {echo '4 days ago';}
else if ($diffhours >= $fivedays && $diffhours $sixdays) {echo '5 days ago';}
else if ($diffhours >= $sixdays && $diffhours $sevendays) {echo '6 days ago';}
else if ($diffhours >= $sevendays && $diffhours $eightdays) {echo '1 week ago';}
else if ($diffhours >= $eightdays && $diffhours $ninedays) {echo '8 days ago';}
else if ($diffhours >= $ninedays && $diffhours $tendays) {echo '9 days ago';}
else if ($diffhours >= $tendays && $diffhours $elevendays) {echo '10 days ago';}
else if ($diffhours >= $elevendays && $diffhours $twelvedays) {echo '11 days ago';}
else if ($diffhours >= $twelvedays && $diffhours $thirteendays) {echo '12 days ago';}
else if ($diffhours >= $thirteendays && $diffhours $fourteendays) {echo '13 days ago';}
else if ($diffhours >= $fourteendays && $diffhours $fifteendays) {echo '2 weeks ago';}
else if ($diffhours $fifteendays) {echo osc_format_date(osc_item_pub_date());}
?>

« Last Edit: February 13, 2014, 05:48:59 am by dev101 »

serjuc11111

  • Hero Member
  • *****
  • Posts: 814
Re: How to show time of any posted item
« Reply #21 on: October 02, 2013, 06:44:05 pm »
it ok but how to display "today"??it is easy to count the difference hours and display this
12 hours ago....for example...
yesterday post ad at 23:00
today at 05:00 i want to displya "posted yesterday" -difference hour are only 6...but the days are different..a diffeerence by 1 sec change the day"yesterday"with "today"

shamim_biplob

  • Full Member
  • ***
  • Posts: 169
Re: How to show time of any posted item
« Reply #22 on: October 16, 2013, 08:11:51 am »
you can use posted_ago plugin. everything is there in a nice way..

see in my site
http://www.banglardokan.com

mkmobi

  • Full Member
  • ***
  • Posts: 119
Re: How to show time of any posted item
« Reply #23 on: February 12, 2014, 06:17:46 pm »
Thanks dev101 really its working great   :)
Here is the much better code I personally wrote! It is actually my first php solution that does something usefull. :)
Of course, thanks to TheDeadLives for the original idea!

Reference points are item's relative publishing Date&Time, not absolute ones, so I dropped ref points like 'today' and 'yesterday' in my code simply because I couldn't figure how to subtract absolute dates, instead of time. After all, relative time is more informative.

Code: [Select]
<?php
$pubdate osc_item_field("dt_pub_date");
$pubdateformat strtotime($pubdate);
$now time();
$diff $now $pubdateformat;
$diffhours floor($diff/3600);
$diffminutes floor($diff/60);
$zero 0;
$oneminute 1;
$sixtyminutes 60;
$onehour 1;
$oneday 24;
$twodays 48;
$threedays 72;
$fourdays 96;
$fivedays 120;
$sixdays 144;
$sevendays 168;
$eightdays 192;
$ninedays 216;
$tendays 240;
$elevendays 264;
$twelvedays 288;
$thirteendays 312;
$fourteendays 336;
$fifteendays 360;

if ($diffminutes == $zero) {echo 'just posted!' ;}
else if ($diffminutes == $oneminute) {echo '1 minute ago' ;}
else if ($diffminutes $oneminute && $diffminutes $sixtyminutes) {echo $diffminutes ' minutes ago' ;}
else if ($diffhours == $onehour) {echo '1 hour ago';}
else if ($diffhours $onehour && $diffhours $oneday) {echo $diffhours ' hours ago';}
else if ($diffhours >= $oneday && $diffhours $twodays) {echo '1 day ago';}
else if ($diffhours >= $twodays && $diffhours $threedays) {echo '2 days ago';}
else if ($diffhours >= $threedays && $diffhours $fourdays) {echo '3 days ago';}
else if ($diffhours >= $fourdays && $diffhours $fivedays) {echo '4 days ago';}
else if ($diffhours >= $fivedays && $diffhours $sixdays) {echo '5 days ago';}
else if ($diffhours >= $sixdays && $diffhours $sevendays) {echo '6 week ago';}
else if ($diffhours >= $sevendays && $diffhours $eightdays) {echo '1 week ago';}
else if ($diffhours >= $eightdays && $diffhours $ninedays) {echo '8 days ago';}
else if ($diffhours >= $ninedays && $diffhours $tendays) {echo '9 days ago';}
else if ($diffhours >= $tendays && $diffhours $elevendays) {echo '10 days ago';}
else if ($diffhours >= $elevendays && $diffhours $twelvedays) {echo '11 days ago';}
else if ($diffhours >= $twelvedays && $diffhours $thirteendays) {echo '12 days ago';}
else if ($diffhours >= $thirteendays && $diffhours $fourteendays) {echo '13 days ago';}
else if ($diffhours >= $fourteendays && $diffhours $fifteendays) {echo '2 weeks ago';}
else if ($diffhours $fifteendays) {echo osc_format_date(osc_item_pub_date());}
?>


dev101

  • Osclass Hero
  • Hero Member
  • *
  • Posts: 2155
  • osclass.work
Re: How to show time of any posted item
« Reply #24 on: February 13, 2014, 05:50:26 am »
Hi mkombi,

I've just noticed and corrected a bug: 6 week -> 6 days.

Regards,
dev101

sirjones

  • Full Member
  • ***
  • Posts: 102
Re: How to show time of any posted item
« Reply #25 on: February 13, 2014, 07:43:17 am »
then how to show ON GOING based Date Interval from Custom fields?

if we input at Custom fields as Date interval let say From 02/13/2014 To 03/13/2014 it should show ON GOING

sirjones

  • Full Member
  • ***
  • Posts: 102
Re: How to show time of any posted item
« Reply #26 on: February 14, 2014, 07:56:14 am »
any idea how to convert dev101's script to the fucntion ?

e.g.
Code: [Select]
<?php
function osc_time_diff () {

//dev101's script here ....


?>

dev101

  • Osclass Hero
  • Hero Member
  • *
  • Posts: 2155
  • osclass.work
Re: How to show time of any posted item
« Reply #27 on: February 14, 2014, 12:00:42 pm »
Hi sirjones,

put this into functions.php before ?> tag:

Code: [Select]
function osc_time_diff() {

$pubdate = osc_item_field("dt_pub_date");
$pubdateformat = strtotime($pubdate);
$now = time();
$diff = $now - $pubdateformat;
$diffhours = floor($diff/3600);
$diffminutes = floor($diff/60);
$zero = 0;
$oneminute = 1;
$sixtyminutes = 60;
$onehour = 1;
$oneday = 24;
$twodays = 48;
$threedays = 72;
$fourdays = 96;
$fivedays = 120;
$sixdays = 144;
$sevendays = 168;
$eightdays = 192;
$ninedays = 216;
$tendays = 240;
$elevendays = 264;
$twelvedays = 288;
$thirteendays = 312;
$fourteendays = 336;
$fifteendays = 360;

if ($diffminutes == $zero) {echo 'just posted!' ;}
else if ($diffminutes == $oneminute) {echo '1 minute ago' ;}
else if ($diffminutes > $oneminute && $diffminutes < $sixtyminutes) {echo $diffminutes . ' minutes ago' ;}
else if ($diffhours == $onehour) {echo '1 hour ago';}
else if ($diffhours > $onehour && $diffhours < $oneday) {echo $diffhours . ' hours ago';}
else if ($diffhours >= $oneday && $diffhours < $twodays) {echo '1 day ago';}
else if ($diffhours >= $twodays && $diffhours < $threedays) {echo '2 days ago';}
else if ($diffhours >= $threedays && $diffhours < $fourdays) {echo '3 days ago';}
else if ($diffhours >= $fourdays && $diffhours < $fivedays) {echo '4 days ago';}
else if ($diffhours >= $fivedays && $diffhours < $sixdays) {echo '5 days ago';}
else if ($diffhours >= $sixdays && $diffhours < $sevendays) {echo '6 week ago';}
else if ($diffhours >= $sevendays && $diffhours < $eightdays) {echo '1 week ago';}
else if ($diffhours >= $eightdays && $diffhours < $ninedays) {echo '8 days ago';}
else if ($diffhours >= $ninedays && $diffhours < $tendays) {echo '9 days ago';}
else if ($diffhours >= $tendays && $diffhours < $elevendays) {echo '10 days ago';}
else if ($diffhours >= $elevendays && $diffhours < $twelvedays) {echo '11 days ago';}
else if ($diffhours >= $twelvedays && $diffhours < $thirteendays) {echo '12 days ago';}
else if ($diffhours >= $thirteendays && $diffhours < $fourteendays) {echo '13 days ago';}
else if ($diffhours >= $fourteendays && $diffhours < $fifteendays) {echo '2 weeks ago';}
else if ($diffhours > $fifteendays) {echo osc_format_date(osc_item_pub_date());}

}

call the function wherever you like:

Code: [Select]
<?php echo osc_time_diff(); ?>

Regards

fog

  • Hero Member
  • *****
  • Posts: 1062
Re: How to show time of any posted item
« Reply #28 on: February 14, 2014, 08:04:46 pm »
Hello, thank you @dev101

for those who want translation, here is the change in the code

For standard ad:
Code: [Select]
<?php
$pubdate osc_item_field("dt_pub_date");
$pubdateformat strtotime($pubdate);
$now time();
$diff $now $pubdateformat;
$diffhours floor($diff/3600);
$diffminutes floor($diff/60);
$zero 0;
$oneminute 1;
$sixtyminutes 60;
$onehour 1;
$oneday 24;
$twodays 48;
$threedays 72;
$fourdays 96;
$fivedays 120;
$sixdays 144;
$sevendays 168;
$eightdays 192;
$ninedays 216;
$tendays 240;
$elevendays 264;
$twelvedays 288;
$thirteendays 312;
$fourteendays 336;
$fifteendays 360;
//$sixteendays = 384;
//$seventeendays = 408;

if ($diffminutes == $zero) {_e(' just posted!''YourThemeName') ;}
else if ($diffminutes == $oneminute) {_e(' 1 minute ago''YourThemeName') ;}
else if ($diffminutes $oneminute && $diffminutes $sixtyminutes) {echo $diffminutes;_e(' minutes ago''YourThemeName') ;}
else if ($diffhours == $onehour) {_e(' 1 hour ago''YourThemeName') ;}
else if ($diffhours $onehour && $diffhours $oneday) {echo $diffhours;_e(' hours ago''YourThemeName') ;}
else if ($diffhours >= $oneday && $diffhours $twodays) {_e(' 1 day ago''YourThemeName') ;}
else if ($diffhours >= $twodays && $diffhours $threedays) {_e(' 2 days ago''YourThemeName') ;}
else if ($diffhours >= $threedays && $diffhours $fourdays) {_e(' 3 days ago''YourThemeName') ;}
else if ($diffhours >= $fourdays && $diffhours $fivedays) {_e(' 4 days ago''YourThemeName') ;}
else if ($diffhours >= $fivedays && $diffhours $sixdays) {_e(' 5 days ago''YourThemeName') ;}
else if ($diffhours >= $sixdays && $diffhours $sevendays) {_e(' 6 days ago''YourThemeName') ;}
else if ($diffhours >= $sevendays && $diffhours $eightdays) {_e(' 1 week ago''YourThemeName') ;}
else if ($diffhours >= $eightdays && $diffhours $ninedays) {_e(' 8 days ago''YourThemeName') ;}
else if ($diffhours >= $ninedays && $diffhours $tendays) {_e(' 9 days ago''YourThemeName') ;}
else if ($diffhours >= $tendays && $diffhours $elevendays) {_e(' 10 days ago''YourThemeName') ;}
else if ($diffhours >= $elevendays && $diffhours $twelvedays) {_e(' 11 days ago''YourThemeName') ;}
else if ($diffhours >= $twelvedays && $diffhours $thirteendays) {_e(' 12 days ago''YourThemeName') ;}
else if ($diffhours >= $thirteendays && $diffhours $fourteendays) {_e(' 13 days ago''YourThemeName') ;}
else if ($diffhours >= $fourteendays && $diffhours $fifteendays) {_e(' 2 weeks ago''YourThemeName') ;}
//else if ($diffhours >= $fifteendays && $diffhours < $sixteendays) {_e(' 15 days ago', 'YourThemeName') ;}
//else if ($diffhours >= $sixteendays && $diffhours < $seventeendays) {_e(' 16 days ago', 'YourThemeName') ;}
else if ($diffhours $fifteendays) {echo osc_format_date(osc_item_pub_date());}
?>

For Premium ad:
Code: [Select]
<?php
$pubdate osc_premium_field("dt_pub_date");
$pubdateformat strtotime($pubdate);
$now time();
$diff $now $pubdateformat;
$diffhours floor($diff/3600);
$diffminutes floor($diff/60);
$zero 0;
$oneminute 1;
$sixtyminutes 60;
$onehour 1;
$oneday 24;
$twodays 48;
$threedays 72;
$fourdays 96;
$fivedays 120;
$sixdays 144;
$sevendays 168;
$eightdays 192;
$ninedays 216;
$tendays 240;
$elevendays 264;
$twelvedays 288;
$thirteendays 312;
$fourteendays 336;
$fifteendays 360;
//$sixteendays = 384;
//$seventeendays = 408;

if ($diffminutes == $zero) {_e(' just posted!''YourThemeName') ;}
else if ($diffminutes == $oneminute) {_e(' 1 minute ago''YourThemeName') ;}
else if ($diffminutes $oneminute && $diffminutes $sixtyminutes) {echo $diffminutes;_e(' minutes ago''YourThemeName') ;}
else if ($diffhours == $onehour) {_e(' 1 hour ago''YourThemeName') ;}
else if ($diffhours $onehour && $diffhours $oneday) {echo $diffhours;_e(' hours ago''YourThemeName') ;}
else if ($diffhours >= $oneday && $diffhours $twodays) {_e(' 1 day ago''YourThemeName') ;}
else if ($diffhours >= $twodays && $diffhours $threedays) {_e(' 2 days ago''YourThemeName') ;}
else if ($diffhours >= $threedays && $diffhours $fourdays) {_e(' 3 days ago''YourThemeName') ;}
else if ($diffhours >= $fourdays && $diffhours $fivedays) {_e(' 4 days ago''YourThemeName') ;}
else if ($diffhours >= $fivedays && $diffhours $sixdays) {_e(' 5 days ago''YourThemeName') ;}
else if ($diffhours >= $sixdays && $diffhours $sevendays) {_e(' 6 days ago''YourThemeName') ;}
else if ($diffhours >= $sevendays && $diffhours $eightdays) {_e(' 1 week ago''YourThemeName') ;}
else if ($diffhours >= $eightdays && $diffhours $ninedays) {_e(' 8 days ago''YourThemeName') ;}
else if ($diffhours >= $ninedays && $diffhours $tendays) {_e(' 9 days ago''YourThemeName') ;}
else if ($diffhours >= $tendays && $diffhours $elevendays) {_e(' 10 days ago''YourThemeName') ;}
else if ($diffhours >= $elevendays && $diffhours $twelvedays) {_e(' 11 days ago''YourThemeName') ;}
else if ($diffhours >= $twelvedays && $diffhours $thirteendays) {_e(' 12 days ago''YourThemeName') ;}
else if ($diffhours >= $thirteendays && $diffhours $fourteendays) {_e(' 13 days ago''YourThemeName') ;}
else if ($diffhours >= $fourteendays && $diffhours $fifteendays) {_e(' 2 weeks ago''YourThemeName') ;}
//else if ($diffhours >= $fifteendays && $diffhours < $sixteendays) {_e(' 15 days ago', 'YourThemeName') ;}
//else if ($diffhours >= $sixteendays && $diffhours < $seventeendays) {_e(' 16 days ago', 'YourThemeName') ;}
else if ($diffhours $fifteendays) {echo osc_format_date(osc_premium_pub_date());}
?>

I use a path for two files without functions (optional):
Code: [Select]
<?php osc_current_web_theme_path('NameOfYourFileStandardAd.php') ; ?>
Code: [Select]
<?php osc_current_web_theme_path('NameOfYourFilePremiumAd.php') ; ?>
Regards

dev101

  • Osclass Hero
  • Hero Member
  • *
  • Posts: 2155
  • osclass.work
Re: How to show time of any posted item
« Reply #29 on: February 14, 2014, 08:31:07 pm »
Hi fog,

translation can be a bit tricky, this really depends on a language(s) used, that is why I scilently ignored it.

For example, there are languages that have different sentance constructions depending on the number, like changing the male/female attribute of a number or some similar property. So, in order to create unified version for translation, users should adjust the granularity of the script to allow those cases to be translated separately in the correct way. But, it is pretty much straightforward, just add some new variables and else if cases. I do not know every language in the world, but I guess there are many exceptions.

Thanks for the idea! ;)

Regards