Good !
I am having some difficulties with extracting data from a date. The thing is that I get a number from an undocumented API.
"created": 734394
"last_chapter_date": 734883
I tried dividing it by 365,242 days (exact amount of days a year)
2010,705231052289
So apparently these are the number of days passed since 0.0.0000
I am currently trying something like that:
http://jsfiddle.net/LRUy5/4/
function zero21970(nDays) {
// 0 70 2013
// |-----|-----|
// 0 to date
var dateMils = nDays*24*60*60*100;
// 0 to 1970
zeroTo1970 = (1970*365.242)*24*60*60*100;
//subtract time from 0-1970 from the time 0-date
//to cut out the part from 1970-today
return new Date(dateMils-zeroTo1970);
}
//http://www.mangaeden.com/api/manga/4e70e9f6c092255ef7004344/
zero21970(734394) //-> Jan 26 1974
I need to save it in a database and work with it via php or javascript..
Does anyone recognize this kind of format or do you know a convenient way of formatting it?
Edit: I should add that the last chapter came out around 15.01.2013.. just to have something to grab.
Updated version:
I guess if the last chapter was from 2013, then the value is a number of days from 01.01.0001. So we can update the initial date as well as change setHours to setDate method for more accuracy:
var date = new Date("0001");
date.setDate(734883);
date.toGMTString(); // "Tue, 15 Jan 2013 00:00:00 GMT"
DEMO: http://jsfiddle.net/LRUy5/6/
Old version:
I found one solution that successfully works at my computer:
var date = new Date("0000");
date.setHours(734394 * 24);
date.toGMTString(); // "Mon, 13 Sep 2010 21:00:00 GMT"
DEMO: http://jsfiddle.net/LRUy5/5/
If you're using PHP, then you should replace
return new Date(dateMils-zeroTo1970);
with
return date('Y-m-d', (dateMils-zeroTo1970));
Related
from php I write like this :
$date = new \DateTime('2019-11-15 23:00:00', new \DateTimeZone('UTC'));
$result = [
'id' => $auction->getId(),
'endDate' => $date,
];
Now in the api I saw :
endDate : 2019-11-15T23:00:00+00:00
In my linux vps when I wrote date I get :
Fri Nov 15 20:27:50 UTC 2019
The problem is that on front I have a countdown, and on every endDate is adding 2 hours and I don't understand why.
Your front will diplay the date in YOUR time zone.
In you database or backend, the date is stored with the default time zone.
Here is an example:
const date = '2019-11-15T23:00:00+00:00'
diffWithYourTimeZone = new Date(date).getTimezoneOffset();
console.log(diffWithYourTimeZone)
// You will probably see 120(min) or -120(min)
If you do console.log(new Date()) in your navigator, you will have something like this xxx xxx xx 2019 HH:MM:MiMi GMT+0200 (...) it means that you are in the zone +2H from GMT(Greenwich Mean Time). When you store data in your backend, it's always as GMT + 0. But for your user in the front, you want to dsplay their date and time. This is why your navigator convert your date in GMT + 0 in the time in GMT + 2.
Set-up
I've decided to try a little bit of PHP myself, I'm a total beginner.
I run a WooCommerce shop and want to dynamically display the expected delivery date range on each product page.
The delivery date range is today's date plus 3 and plus 4 days, e.g. today is the of 10th October so the delivery date range is 13 and 14 October. On each product page it should therefore mention,
Delivery between Oct 13 and Oct 14
I know where to write the code such that it displays where I want it to appear on the product pages.
I also know how to dynamically update the delivery dates.
Problem
Echo mixes the variables, instead of,
Delivery between Oct 13 and Oct 14
it states,
Delivery between and Oct 13Oct 14
My Code
function my_custom_action() {
$plus3 = strtotime("+3 day");
$plus4 = strtotime("+4 day");
$date_low = date('M d', $plus3);
$date_high = date('M d', $plus4);
$start_text = _e('Delivery between ','woodmart');
$end_text = _e(' and ','woodmart');
echo $start_text, $date_low, $end_text, $date_high ;
};
add_action( 'woocommerce_single_product_summary', 'my_custom_action', 15 );
I need $start_text and $end_text in functions so the strings are translatable.
Preferably I also have the months (e.g. Oct) translatable, but for now I'd already be very happy to know how I get all variables echoed in the desired order.
This can be resolved using string interpolation.
for eg.
$name = "PHP";
echo "I am reading {$name}POT";
// output: I am reading PHPPOT
here's the link to a much more detailed reference : variable-interpolation-in-php
I have this time string from sql 00:05:00 and I need a timestamp from it but can't seem to get it. I have also tried strtotime.
What am I doing wrong?
$NonBillable = '00:05:00';
$NonBillable = DateTime::createFromFormat('H:i:s', $NonBillable)->getTimestamp();
echo $NonBillable;
$NonBillable = DateTime::createFromFormat('H:i:s', '00:05:00');
echo $NonBillable->getTimestamp();
It results for me 1464480300 -> Sun, 29 May 2016 00:05:00 GMT because no date has been given, it uses today.
So a simple solution to this would be:
echo $NonBillable->getTimestamp() - time();
I'm hoping that my concern will be answered as soon as possible. :) So here it is: I'm having a hard time solving this problem, as shown below:
$beg_week = "2014 - 49"; //yyyy - w
$stop_wk = "2015 - 5"
while($beg_week <= $stop_wk)
{
/* do logic to add week in the format "yyyy - w", but also
* have to consider the year */
$beg_week = '';
}
So, how can I add a week in a 'yyyy - w' format, without using strtotime()?
Convert your start and end times to timestamps using date_parse_from_format() and mktime(). Alternatively, use an SQL function like MySQL's UNIX_TIMESTAMP() if retrieving data from a datetime field.
Use date_parse_from_format() to break the date into its components.
Use mktime() to get a timestamp
Add a week's worth of seconds (60 * 60 * 24 * 7)
Use date() to output the next week.
Note: date_parse*() won't log/store an error in the returned array if you have more than 29 or 28 days in February (for a leap or regular year, respectively). This might/might not matter, depending on what you're using it for.
There is no need to jump through hoops with date() and mktime() for this. The DateTime classes can handle it simply and cleanly, something like this should work for you:-
$beg_week = (new \DateTime())->setISODate(2014, 49);
$stop_week = (new \DateTime())->setISODate(2015, 5);
$interval = new \DateInterval('P7D');
while($beg_week < $stop_week){
echo $beg_week->format('Y-m-d') . "<br/>\n";
$beg_week->add($interval);
}
Sorry it took so long before I am able to solved and share the method/approach that I used for this matter, since I've got an additional project than this. So what i did was:
First, build a function that gets the max week of a year(thanks to #salathe),
function getIsoWeekYear($year)
{
$date = new DateTime;
$date->setISODate($year,53);
return ($date->format("W") === "53" ? 53 : 52);
}
Then to increment the value of a week, considering also the given year,
$beg_week = "2014 - 50"; //just a sample and not actually a string
$end_week = "2015 - 05";
while($beg_week<=$end_week)
(
$out_data[] = $beg_week;
$b_week_exp = explode(" - ",$beg_week);
$b_yr_temp = $b_week_exp[0];
$b_wk_temp = $b_week_exp[1];
$max_wk_of_yr = getIsoWeeksInYear($b_yr_temp);
$out_year = $b_yr_temp;
$out_week_no = $b_wk_temp+1;
if($out_week_no > $max_wk_of_yr)
{
$out_year = $b_yr_temp+1;
$out_week_no = "1";
}
$beg_week = $out_year." - ".sprintf("%02s", $out_week_no);
)
That's it, if you will print_r the $out_data, you will have an array of,
2014 - 50
2014 - 51
2014 - 52
2015 - 01
2015 - 02
2015 - 03
2015 - 04
2015 - 05
Well, this logic is what I want, to have loop from $beg_week up to $end_week, because there's also a logic that I am executing in it. A very simple trick, for this very simple problem! :) Sorry, SOMETIMES I am that sluggish not to answer my own question/problem. I hope this one will help to anyone who'll also encounter this same scenario. Thank you!
my client is proving me with date strings as follows
Tue Nov 30 00:00:00 GMT+0400 1965
how can i insert this format into a postgresql Date column please, or will i have to do it the hard way and use the sub strings to compile a string of
Nov 30 1965
Please can someone help im very stuck for time.
Thanks in advance.
It would be possible for you, if you date have no timestamp in it:
select to_timestamp('Tue Nov 30 00:00:00 1965', 'DY Mon DD HH24:MI:SS YYYY')
So I think you have to make a new from your string:
select
to_timestamp(substring(dt from 0 for 20) || substring(dt from 29), 'DY Mon DD HH24:MI:SS YYYY')
from (select 'Tue Nov 30 00:00:00 GMT+0400 1965'::text as dt) as a
sql fiddle demo
ive managed it this way
function dateStringToDb($dateString){
if($dateString != NULL){
$dateString = substr($dateString,4,6)." ".substr($dateString,sizeof($dateString)-5,4);
return new Zend_Db_Expr("to_date('".$dateString."', 'Mon DD YYYY')");
}
return NULL;
}
this seems to be the best method, thanks anyway