distinct date mysql php year - php

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>");
}
?>

Related

how to pass array into database

I am trying to pass arrays into a database but can't get it right.
I have following db rows
Country, number, Page, date
And want something like this in my database
id Country number Page date
2 Sweden 5 cat 14th june 2015
United 10 ind 14th june 2015
states 58 con 14th june 2015
France 101 aces 14th june 2015
Germany 200 ind 14th june 2015
I am using this
if(isset($_POST['submit'])){
$new_res = mysql_query("INSERT INTO `em-res_db` VALUES('','$Country','$number','$Page','$date')");
if ($new_res){
//continue
}
I hope you get the picture of what I'm trying to do
Create a loop who have the same length as the array, and in the loop insert the code where you are inserting data to the db.
Example
for(i=0;i<=count(array)-1;i++){
if(isset($_POST['submit'])){
$new_res = mysql_query("INSERT INTO `em-res_db` VALUES('','$Country','$number','$Page','$date')");
if ($new_res){
//continue
}
}

Adding and removing from a string containing dates using PHP

I have to edit a whole bunch of date intervals. But they are all mixed up. Most are in the form Month YearMonth Year
eg January 2014March 2015
How would I insert a hyphen in between so I end up with
January 2014 - March 2015
I also have the problem where these dates occur in the same year.
eg April 2012September2012
In such a case I would need to insert the hyphen and remove the year so that I'm left with
April - September
There must be some PHP string operators for stuff like this. Well thats what I'm hoping.
Would appreciate some guidance. Thanks in advance.
Thanks, sorry for my delayed reply
$string = "January 2014March 2015";
preg_match('/([a-z]+) *(\d+) *([a-z]+) *(\d+)/i', $string, $match);
print "$match[1] $match[2] - $match[3] $match[4]";
outputs,
January 2014 - March 2015
You could do it using lookaround:
$string = "January 2014March 2015";
$res = preg_replace('/(?<=\d)(?=[A-Z])/', ' - ', $string);
echo $res,"\n";
Output:
January 2014 - March 2015

How to ordering name of month in MySQL & PHP?

I have a query for showing my database :
SELECT archieve, SUM(items_in) AS income, SUM(items_out+black_out+white_out) AS outcome, SUM((ball_out+black_out+white_out
)-items_in) AS efficiency, SUM((items_in / ( ball_out + black_out + white_out))*100) AS percent
FROM items
GROUP BY archieve
ORDER BY DATE_FORMAT(archieve,'%m')
my archieve table just content month and year of data when it's submitted. For example :
Jan 2014, Feb 2014, Mar 2014
but what showed in my page is :
Feb 2014, Jan 2014, Mar 2014
This my php code to insert data from my web into database :
$date_trans = date("d M Y");
$archieve = date("M Y");
$items_in = mysql_real_escape_string($_POST['items_in']);
$ball_out = mysql_real_escape_string($_POST['ball_out']);
$black_out = mysql_real_escape_string($_POST['black_out']);
$white_out = mysql_real_escape_string($_POST['white_out']);
$ball = mysql_real_escape_string($_POST['ball']);
$black = mysql_real_escape_string($_POST['black']);
$white = mysql_real_escape_string($_POST['white']);
$note = mysql_real_escape_string($_POST['note']);
Everything's fine with the code, but how to make it ordered well by month? Because I've tried with MONTH(date) or date.(MONTH) it doesn't work. Thanks in advance ;)
You need to convert your string "archieve" to date first, and then sort by the value.
Try this:
ORDER BY STR_TO_DATE(archieve, '%M %Y')
try this
ORDER BY FIELD(archieve,'Jan','Feb','Mar',...)
or this
ORDER BY FIELD(DATE_FORMAT(archieve,'%m'),'01','02','03',...)

Date String from php to postgresql

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

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