I need to format the pubDate to example :
2 hours ago, 2 days ago or 20 hours ago ....
while keeping the original date of the feed cause i tried to format before and i get all my feeds same pubDate the one i set in Date format and the date builder. How can i solve this issue ?
As well i tried to format the text of my pubdate and (Read full article ) when i run pipe its perfect but in pipe output and when i call the rss file from my website i always get the format as code.
You help will be greatly appreciated...
function ShowDate($date) // $date --> time(); value
{
$stf = 0;
$cur_time = time();
$diff = $cur_time - $date;
$phrase = array('second','minute','hour','day','week','month','year','decade');
$length = array(1,60,3600,86400,604800,2630880,31570560,315705600);
for($i =sizeof($length)-1; ($i >=0)&&(($no = $diff/$length[$i])< =1); $i--); if($i < 0) $i=0; $_time = $cur_time -($diff%$length[$i]);
$no = floor($no); if($no <> 1) $phrase[$i] .='s'; $value=sprintf("%d %s ",$no,$phrase[$i]);
if(($stf == 1)&&($i >= 1)&&(($cur_tm-$_time) > 0)) $value .= time_ago($_time);
return $value.' ago ';
}
You need to pass the date within that and you can get the desire format.
This is php function that will convert the time as a 2 hours ago and so on...
For more further function you can check this link Date time formate
Related
I am trying to set up a timestamp range for a file that is not over 2 hours in Perl. Taking times from file are formatted as "May 26 20:30 filename.csv" in perl format.
Here is what I am setting up:
use File::stat;
my $stat = stat($file);
# To start a time within a range or no more than 2 hours
my $start = UnixDate("now", "%m%d %H:%M");
my $stop = UnixDate("$start"+2, "%m%d %H:%M"); // here max it to 2 hours, not sure this part
if ($stat->$start <= $stat->$stop) { next; }
Since you say that UnixDate returns an epoch timestamp, all you need is this:
my $cutoff = time - 2*60*60;
my $file_time = UnixDate( "%m%d %H:%M", $fn );
next if $file_time < $cutoff;
My example was: How to compare dates using perl?
...
# $ARG1 is specified by the user for example "10" min
my $difftime=$ARG1;
# -- Get date ---------------------------------------------------------------------
#Stores current date and time - $time min
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =localtime(time() - (60*$difftime));
$year += 1900;
#parse date format to '22.10.2021 00:00:00' -----------------------------------------------------
my $act_date="$mday.$mon.$year $hour:$min:$sec";
...
My first problem is $hour and $mon are one to low why?
My second problem is the time is with one digit "8:4:34" and not with two digits "08:04:34". Is that a problem for a date comparison?
...
#compare date
# date example for $sql_value is 06.10.2021 09:38:27
my $date_to_compare=$sql_value;
if ($act_date <= $date_to_compare)
{
print "the current date is smaller";
}
else
{
print "the current date is greater";
}
...
I get only "the current date is smaller" Why?
Additionally how can i get the time different in minutes?
thanks
Dealing with dates and times at a low level like you're trying to do is really making your life far harder than it needs to be. You should follow the advice in the answers to the example that you link to and use Time::Piece.
My first problem is $hour and $mon are one to low why?
If $hour is wrong then that's likely to be down to timezone differences. But $mon is defined to be between 0 and 11 in the documentation for localtime(). This is part of what I mean by making your life too hard by working at this level.
You really haven't made it clear what you're trying to do. But you can make Time::Piece objects from a datetime string using strptime().
my $date_str = '22.10.2021 00:00:00';
my $date = Time::Piece->strptime($date_str, '%d.%m.%y %H:%M:%S');
Two Time::Piece objects can be compared directly:
if ($dt1 > $dt2) {
# do something
} else {
# do something else
}
If you subtract two Time::Piece objects, you get the number of seconds between the two timestamps.
my $secs = $dt1 - $dt2;
And you can use the strftime() method to get your timestamp in whatever format you want.
say $dt1->strftime('%d.%m.%y %H:%M:%S');
I have this Perl script where I need to monitor the execution time of DBI calls.
In Europe (France), I have no problem: 2 seconds execution time is reported 2 seconds.
This same script running on a computer in Singapore reports 30 minutes and 2 seconds.
Why ?
use strict;
use Time::Format qw(%time);
use Time::HiRes qw(gettimeofday);
my $time_start = gettimeofday();
sleep 2; # some action goes here
my $stat_perf = gettimeofday() - $time_start;
print STDOUT $time{'mm:ss.mmm', $stat_perf} . " \n";
The output in France is
00:02.000
The same script running in Singapore yields:
30:02.001
Why ?
According to this documentation, the gettimeofday function returns seconds or microseconds since the unix epoch, which is 1/1/1970 UTC. Because it is in UTC, it is not affected by time zones at all.
Also, in your original code you are just using gettimeofday, which is going to be returning timestamps from now, not from 1970. But in your suggested answer, for some reason, you have hard-set the timestamp, which won't help you do much.
Yes, there is history to just about every time zone, including Singapore. You can see it in the TZDB here. But you are incorrect about it being +8:30 at the epoch. It was actually +7:30. You can verify also on this site. But it doesn't matter anyway because like I said, gettimeofday works strictly in UTC.
I think the problem is in how you are interpreting the results. You have as your last line:
print STDOUT $time{'mm:ss.mmm', $stat_perf} . " \n";
But $stat_perf is the elapsed duration of time, not a value that you can treat as a timestamp. You probably shouldn't be passing it to $time, since that will use the local time zone and be expecting a full timestamp.
Also, you may want to use tv_interval, as shown in the examples.
Update
I searched through the CPAN archives and I'm sure somewhere there is a module for formatting an elapsed duration of time, but I can't seem to find it. Anyway, it's not too hard to write this on your own. Here, this should work:
my $min = $stat_perf / 60;
my $sec = ($stat_perf * 1000 % 60000) / 1000;
my $elapsed = sprintf("%02u:%06.3f", $min, $sec);
print STDOUT $elapsed . "\n";
The anser is ...
Singapore is now 08h00 offset from UTC. In 1970, it was offset by 08h30. Asking for the conversion of a few seconds into a string will get us to 1970, not today's date, and timezone.
By requesting
print STDOUT $time{'mm:ss.mmm', 2} . " \n";
the system adjusts to 1970 (epoch) timezone offset.
In order to get a correct result in Singapore, we must shift to after 1982, when Singapore made its last timezone change.
print STDOUT $time{'mm:ss.mmm', 2 + 1356994800} . " \n";
as
UNIX_TIMESTAMP('2013-01-01 00:00:00') = 1356994800
We are only concerned by the time of day portion of the date, so this does it.
Check with
zdump -v Asia/Singapore
This is the trick.
Here is a script that emulates $time{} in converting a real number into a string representing the mm:ss sexagesimal conversion of its integer part, concatenated with the decimal remainder formatted as microseconds.
As this is going to be part of a library, there are protections set to avoid invoking it with bad arguments.
I hope I didn't miss something.
use strict;
use Time::Format qw(%time);
# ----------------------------------------------------------
# A substitute to $time{} as we have issues with TZ offsets at epoch days in some part of the World
# A real to sexagesimal converter
# Format will be set to match $time{'mm:ss.mmm', $stat_perf};
sub microTime {
return '' unless (my ($intertime) = #_);
return '' unless (ref ($intertime) eq '');
return '' unless (sprintf("%s", $intertime) =~ m/^(?:[\d]+)(?:\.(?:[\d]+))?$/);
my $intNum = int($intertime);
"a" =~ /a/; # Resets regex buffers
sprintf ("%.03f", $intertime - $intNum) =~ m,\.([\d]+),;
my $intDec = $1; # It's always defined
my $intUnder = $intNum % 3600;
my $intMin = int($intUnder / 60);
my $intSec = $intUnder % 60;
return sprintf ("%02d:%02d.%03d", $intMin, $intSec, $intDec);
}
my $stat_perf;
$stat_perf = 345.987;
$stat_perf = 345;
$stat_perf = 3945.987;
$stat_perf = 0;
$stat_perf = 3945.918733;
print STDOUT sprintf (" >> %s\n", µTime ($stat_perf));
print STDOUT sprintf (" == %s\n", $time{'mm:ss.mmm', $stat_perf});
How do I get the closest date that is earlier than the input date or equal to from an array using an input date?
For example, my array would look like this.
#dates = ("200811","200905","200912","201005","201202");
and my input date is
$inputdate = "201003";
How do I get the closest date in the array which is "200912".
The format of the date is YEARMM.
Thanks
Sort the dates, select only the ones preceding the input date, take the last such one:
print ((grep $_ <= $inputdate, sort #dates)[-1]);
use List::Util qw( max );
my $date = max grep { $_ <= $inputdate } #dates;
The logic here is to go one year back and change month from January to December if the month is January, otherwise go back one month in the same year.
I don't code much in Perl, the code in PHP is:
(I'm putting it here to give you the logic. Coding it should be trivial)
$dates = array("200811","200905","200912","201005","201202");
$inputdate = "201003";
$date = $inputdate;
while ($found==0) {
if (in_array($date, $dates)) {
$found = 1;
echo "the date is " . $date;
}
if ($date%100==1) { // if it's january, we need to change to december of the previous year
$date = $date - 100 + 12;
}
else {
$date = $date - 1; //go one month back in the same year
}
}
I'm using Date::Manip for a variety of things, and want to create an array of days of the month.
I think I need:
#date = &ParseRecur("2010:4:0:0:0:0:0");
but, it doesn't do it.
I've read & reread the man page but can't get the syntax.
#date = &ParseRecur("2010:4:0:1:0:0:0");
#date = &ParseRecur("2010:4:0:1*:0:0:0");
don't work either!
You could build the list with your own loop, instead of using ParseRecur.
$month = 4;
for ($day = 1; $day <= 31; $day++) {
my $date = UnixDate( "$month/$day/2010", "%m-%d-%Y" );
push( #list, $date ) if (defined $date);
}
From the man pages:
"There are a small handful of English strings which can be parsed in place of a numerical recur description."
Check out the examples in the man page.
So, if you want an array of days of a month - say for June in 2010 you would do:
#dates = ParseRecur("every day in June 2010");