Date String from php to postgresql - php

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

Related

Using strtotime with TimeZone

I have a problem. I want to convert this DateTime: 2018-10-28 02:00:00 to a TimeStamp. Now the TimeStamp I am looking for is: 1540684800, but with my code I get this TimeStamp: 1540688400. I know it has something to do with my TimeZone, but I don't know how I can fix this.
I live in the Netherlands in Amsterdam.
Here is my code:
$LoopDateTime = "2018-10-28 02:00:00";
$search_key = (strtotime($LoopDateTime)*1000);
Can someone help me?
The time zone identifier for Amsterdam is Europe/Amsterdam and 1540688400 is the correct timestamp. There's surely an online tool to check but you can also verify it from PHP itself:
$date = new DateTime("#1540688400");
$date->setDateTimeZone(new DateTimeZone('Europe/Amsterdam'));
echo $date->format('r'); // Sun, 28 Oct 2018 02:00:00 +0100
However your code is not robust because depends on the configured timezone. You can just set it explicitly in a number of ways, e.g.:
$LoopDateTime = "2018-10-28 02:00:00";
$search_key = strtotime($LoopDateTime . ' Europe/Amsterdam') * 1000;
var_dump($search_key); // int(1540688400000)
Or:
date_default_timezone_set('Europe/Amsterdam');
$LoopDateTime = "2018-10-28 02:00:00";
$search_key = strtotime($LoopDateTime) * 1000;
var_dump($search_key); // int(1540688400000)
P.S. If I'm not wrong Sunday 28 Oct 2018 02:00:00 +0100 is the exact moment when most Europe has just switched from CEST (+0200) to CET (+0100).

PHP create from format not formatting time correctly

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();

distinct date mysql php year

I have problem with my php/mysql.
I want to get distinct year from my fields ex 2013-06-20.
Now I get something like this :
2014
2013
2014
2013
2013
2014
2014
2014
2014
2014
2014
2014
PHP CODE :
<?$chuj=mysql_query("SELECT DISTINCT date_issue FROM invoices_sales ");
while($chuje=mysql_fetch_object($chuj)){
$test=explode("-",$chuje->date_issue);
print_r($test['0']."<br>");
}
?>
How I can get only once 2013 or 2014 year ?
You need to grab the year from that particular date string.
SELECT DISTINCT YEAR(date_issue) FROM invoices_sales
This will ensure it's only comparing the year, as it stands, each of those strings likely have different dates, so it is evaluating the entire date as opposed to only the year.
<?php
$chuj=mysql_query("SELECT DISTINCT YEAR(`date_issue`) as `yr` FROM `invoices_sales`");
while($chuje=mysql_fetch_object($chuj)) {
$test = $chuje->yr;
print_r($test['0'] . "<br>");
}
?>

Bootstrap Datetimepicker JS Format Date Output

i am using bootstrap-datetimepicker.min.js which originally from here (http://tarruda.github.io/bootstrap-datetimepicker/)
when i click some button, the ajax is returning datetime in this format:
Thu Jul 18 2019 07:00:00 GMT+0700 (SE Asia Standard Time)
how to make this format to yyyy-mm-dd so it can fit in mysql database?
i've see some function inside the js file there is formatDate(d) function. But there's no documentation regarding that function..
code for getting the result
var expired_date= $('#dateTimeExpired').data('datetimepicker');
exp_date = expired_date.getDate();
Is there any helps?
thanks in advance.
var dateRaw = "Thu Jul 18 2019 07:00:00 GMT+0700 (SE Asia Standard Time)";
var dateObject = new Date(dateRaw);
var dateFormatted = dateObject.toISOString().substring(0, 10);
alert(dateFormatted); // 2019-07-18
http://jsfiddle.net/eymA4/

Unrecognized Date format

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));

Categories