Php Excel DateStamd Formatted date - phpexcel

I am reading an xlsx file and the fields are in DateStamd I want to convert them to a date data here I leave my progress
Excel A1 = 43160
Converte date = 01/03/2018
enter code hererequire '../PHPExcel-1.8/Classes/PHPExcel/IOFactory.php';
require '../cnx/conection.php';
$objPHPExcel = PHPExcel_IOFactory::load('../../date.xlsx');
$objPHPExcel->setActiveSheetIndex(0);
$numRows = $objPHPExcel->setActiveSheetIndex(0)->getHighestRow();
for ($i = 2; $i <= $numRows; $i++) {
$pm = $objPHPExcel->getActiveSheet()->getCell('A'.$i)->getValue();
echo $pm;
$sql = "INSERT INTO regfecha (reg_fecha) VALUES('$pm')";
$result = $mysqli->query($sql);
}
`
here I have another example and I do not know how to apply it
`
DATEVALUE
Converts a date in the form of text to a serial number.`
<?php
set_include_path(get_include_path() . PATH_SEPARATOR . '../../../../Classes/');
include 'PHPExcel.php';
$objPHPExcel = new PHPExcel();
$worksheet = $objPHPExcel->getActiveSheet();
// Add some data
$testDates = array( '26 March 2012', '29 Feb 2012', 'April 1, 2012', '25/12/2012',
'2012-Oct-31', '5th November', 'January 1st', 'April 2012',
'17-03', '03-2012', '29 Feb 2011', '03-05-07',
'03-MAY-07', '03-13-07',
);
$testDateCount = count($testDates);
for($row = 1; $row <= $testDateCount; ++$row) {
$worksheet->setCellValue('A'.$row, $testDates[$row-1]);
$worksheet->setCellValue('B'.$row, '=DATEVALUE(A'.$row.')');
$worksheet->setCellValue('C'.$row, '=B'.$row);
}
$worksheet->getStyle('C1:C'.$testDateCount)
->getNumberFormat()
->setFormatCode('yyyy-mmm-dd');
echo '<hr />';
// Test the formulae
?>
<p><strong>Warning: </strong>The PHPExcel DATEVALUE() function accepts a wider range of date formats than MS Excel's DATEFORMAT() function.</p>
<table border="1" cellspacing="0">
<tr>
<th>Date String</th>
<th>Formula</th>
<th>Excel DateStamp</th>
<th>Formatted DateStamp</th>
</tr>
<?php
for ($row = 1; $row <= $testDateCount; ++$row) {
echo '<tr>';
echo '<td>' , $worksheet->getCell('A'.$row)->getFormattedValue() , '</td>';
echo '<td>' , $worksheet->getCell('B'.$row)->getValue() , '</td>';
echo '<td>' , $worksheet->getCell('B'.$row)->getFormattedValue() , '</td>';
echo '<td>' , $worksheet->getCell('C'.$row)->getFormattedValue() , '</td>';
echo '</tr>';
}
?>

Related

See last order on specific product

Hi how to see last order of specific product in woocommerce?
EXEMPLE:
PRODUCT NAME
PRICE 10 $ [ADD CART]
LAST ORDER
DATE QUT. PRICE
10.10.2021 2 9$
10.12.2021 6 3$
10.01.2022 39 5$
EDIT: I would like to place it on the product page and as a second viewer user sees his latest product orders.
Place the following function in your functions.php file
function latest_order_by_product_id() {
global $wpdb;
// Select Product ID
$product_id = 14;
$orders_statuses = "'wc-completed', 'wc-processing', 'wc-on-hold'";
//Change get_row to get_col if you want all orders
//Change DESC to ASC if we need reverse order
$orders = $wpdb->get_row("
SELECT DISTINCT woi.order_id FROM {$wpdb->prefix}woocommerce_order_itemmeta as woim,
{$wpdb->prefix}woocommerce_order_items as woi,
{$wpdb->prefix}posts as p WHERE woi.order_item_id = woim.order_item_id AND woi.order_id = p.ID
AND p.post_status IN ( $orders_statuses ) AND woim.meta_key IN ( '_product_id', '_variation_id' ) AND woim.meta_value LIKE '$product_id' ORDER BY woi.order_item_id DESC");
if($orders):
$data = array();
foreach($orders as $key => $order_id):
$order = wc_get_order( $order_id );
if ( $order ):
$data[$key]['order_date'] = $order_date = $order->get_date_completed(); // Change depending on type of date you are looking for - get_date_created(), get_date_completed(), get_date_paid()
//Loop order items
foreach ( $order->get_items() as $item_id => $item ):
if($product_id == $item->get_product_id()):
$data[$key]['qty'] = $product_qty = $item->get_quantity();
//Choose which price you want to get
$data[$key]['price'] = $product_price = $item->get_total() + $item->get_total_tax(); // Discounted total with tax
//OR
//$product_price = $item->get_subtotal() + $item->get_subtotal_tax(); // NON discounted total with tax
endif;
endforeach;
endif;
endforeach;
endif;
if($data):
$output = array();
$output[] .='<table><thead><tr><th>LAST ORDER DATE</th><th>QTY</th><th>PRICE</th></tr></theead><tbody>';
foreach($data as $order_data):
//Change date format if needed
$date = $order_data['order_date']->date("d.m.Y");
$output[] .= '<tr><td>'.$date.'</td><td>'.$order_data['qty'].'</td><td>'.$order_data['price'].'</td></tr>';
endforeach;
$output[] .= '</tbody></table>';
endif;
echo sprintf('%s',implode($output));
}
Call latest_order_by_product_id() where you need it. Result - https://prnt.sc/6AhgTPXFerBg
Example with shortcode. How to use [lobpid product_id="35"]
add_shortcode('lobpid','latest_order_by_product_id' );
function latest_order_by_product_id($attr) {
global $wpdb;
$args = shortcode_atts( array(
'product_id' => '',
), $attr );
// Select Product ID
$product_id = $args['product_id'];
//Skip the rest if we dont have product id defined
if(empty($product_id)): echo 'there is no product id defined'; return; endif;
$orders_statuses = "'wc-completed', 'wc-processing', 'wc-on-hold'";
//Change get_row to get_col if you want all orders
//Change DESC to ASC if we need reverse order
$orders = $wpdb->get_row("
SELECT DISTINCT woi.order_id FROM {$wpdb->prefix}woocommerce_order_itemmeta as woim,
{$wpdb->prefix}woocommerce_order_items as woi,
{$wpdb->prefix}posts as p WHERE woi.order_item_id = woim.order_item_id AND woi.order_id = p.ID
AND p.post_status IN ( $orders_statuses ) AND woim.meta_key IN ( '_product_id', '_variation_id' ) AND woim.meta_value LIKE '$product_id' ORDER BY woi.order_item_id DESC");
//Skip the rest if we dont have orders
if(empty($orders)): echo 'no orders found'; return; endif;
if($orders):
$data = array();
foreach($orders as $key => $order_id):
$order = wc_get_order( $order_id );
if ( $order ):
$data[$key]['order_date'] = $order_date = $order->get_date_completed(); // Change depending on type of date you are looking for - get_date_created(), get_date_completed(), get_date_paid()
//Loop order items
foreach ( $order->get_items() as $item_id => $item ):
if($product_id == $item->get_product_id()):
$data[$key]['qty'] = $product_qty = $item->get_quantity();
//Choose which price you want to get
$data[$key]['price'] = $product_price = $item->get_total() + $item->get_total_tax(); // Discounted total with tax
//OR
//$product_price = $item->get_subtotal() + $item->get_subtotal_tax(); // NON discounted total with tax
endif;
endforeach;
endif;
endforeach;
endif;
if($data):
$output = array();
$output[] .='<table><thead><tr><th>LAST ORDER DATE</th><th>QTY</th><th>PRICE</th></tr></theead><tbody>';
foreach($data as $order_data):
//Change date format if needed
$date = $order_data['order_date']->date("d.m.Y");
$output[] .= '<tr><td>'.$date.'</td><td>'.$order_data['qty'].'</td><td>'.$order_data['price'].'</td></tr>';
endforeach;
$output[] .= '</tbody></table>';
endif;
echo sprintf('%s',implode($output));
}

Woocommerce email invoice, shipping method style by instance_id

In woocommerce I have one shipping zone and under it, there is seven shipping methods. Shipping method ids are 4 (local pick up), 6, 7, 8, 9, 10, 10. Last six are delivery. So in first, local pick up, I want to show only shipping method and in others show shipping ,ethod and shipping address
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
$text_align = is_rtl() ? 'right' : 'left';
$address = $order->get_formatted_billing_address();
$shipping = $order->get_formatted_shipping_address();
$current_shipping_method_id = '';
$current_shipping_method = '';
$type = '';
foreach($order->get_items('shipping') as $item){
$current_shipping_method_id = $item->get_instance_id().'<br>';
}
$shipping_packages = WC()->cart->get_shipping_packages();
foreach( array_keys( $shipping_packages ) as $key ) {
if( $shipping_for_package = WC()->session->get('shipping_for_package_'.$key) ) {
if( isset($shipping_for_package['rates']) ) {
foreach ( $shipping_for_package['rates'] as $rate_key => $rate ) {
$method_id = $rate->method_id;
$instance_id = $rate->instance_id;
if($instance_id == 4){ // Saņemt veikalā
$current_shipping_method = $order->get_shipping_method();
$type = 4;
}
if($instance_id == 6){ // Piegāde darba dienās 9.00-17.00 (nesaskaņots, pēc servisa
vēlmēm)
$current_shipping_method = $order->get_shipping_method().' ('.$order->get_shipping_address_1().' '.$order->get_shipping_address_2().')';
$type = 6;
}
if($instance_id == 7){ // Piegāde darba dienās 9.00-17.00 (Saskaņots pēc klienta vēlmēm)
$current_shipping_method = $order->get_shipping_method().'('.$order->get_shipping_address_1().' '.$order->get_shipping_address_2().')';
$type = 7;
}
if($instance_id == 8){ // Bezmaksas piegāde
$current_shipping_method = $order->get_shipping_method().' ('.$order->get_shipping_address_1().' '.$order->get_shipping_address_2().')';
$type = 8;
}
if($instance_id== 9){ // Omniva pakomāts
$current_shipping_method = $order->get_shipping_method().' ('.$order->get_shipping_address_1().' '.$order->get_shipping_address_2().')';
$type = 9;
}
if($instance_id == 10){ // Omniva kurjers
$current_shipping_method = $order->get_shipping_method().' ('.$order->get_shipping_address_1().' '.$order->get_shipping_address_2().')';
$type = 10;
}
if($instance_id == 11){ // DPD pakomāts
$current_shipping_method = $order->get_shipping_method().' ('.$order->get_shipping_address_1().' '.$order->get_shipping_address_2().')';
$type = 11;
}
}
}
}
}
?>
<div class="p2">
<table class="table2">
<tr>
<th>Pasūtītājs:</td>
<td><?php echo $order->get_billing_first_name().' '.$order->get_billing_last_name(); ?>
</td>
</tr>
<tr>
<th>Kontakti:</th>
<td>
<a href="mailto:<?php echo $order->get_billing_email(); ?>"><?php echo esc_html(
$order->get_billing_email() ); ?></a>,
<?php echo wc_make_phone_clickable( $order->get_billing_phone() ); ?>
</td>
</tr>
<tr>
<th>Preces saņemšana</th>
<td><?php echo $order->get_shipping_method(); ?></td>
</tr>
<?php
if($type == 6 || $type == 7 || $type == 8 || $type == 9 || $type == 10 || $type == 11){
?>
<tr>
<th>Saņemšanas vieta:</th>
<td><?php echo $order->get_shipping_address_1().' '.$order->get_shipping_address_2(); ?>
</td>
</tr>
<?php
}
?>
</table>
<?php echo $type; ?>
</div>
After new order, which is selected as local pick up, instance id 4, in email shows that current instance id is 4, but $type 11 why? I don't understand!
Ok, I did it different, but still don't understand why it's not working with type method
Now it's working:
<?php
$instance_ids = array(6, 7, 8, 9, 10, 11);
if(in_array($current_shipping_method_id, $instance_ids)){
?>
<tr>
<th>Saņemšanas vieta:</th>
<td><?php echo $order->get_shipping_address_1().' '.$order->get_shipping_address_2(); ?></td>
</tr>
<?php
}
?>

Incorrect values returned from $this->$xxxx fields

I have a problem with getting shortcode values into variables, you advise please?
This is the code:
class virtual_cheshire_ring
{
public $start_date = '';
public $end_date = '';
public $db_table_participants = '';
public $db_table_log = '';
public function __construct()
{
add_shortcode('virtual_crr', array($this, 'virtual_crr') );
}
public function virtual_crr($atts)
{
//******************************
//** Get shortcode parameters **
//******************************
global $post;
$shortcode_defaults = [ 'start_date' => '2020-01-01',
'end_date' => '2050-00-01',
'db_table_participants' => 'db_table_participants',
'db_table_log' => 'db_table_log',
];
$attributes = array_merge($shortcode_defaults,$atts);
$this->$start_date = $attributes['start_date'];
$this->$end_date = $attributes['end_date'];
$this->$db_table_participants = $attributes['db_table_participants'];
$this->$db_table_log = $attributes['db_table_log'];
var_dump($attributes);
$html = 'start_date = ' . $this->$start_date . ' / ' . $attributes['start_date'] . '<br>';
$html .= 'end_date = ' . $this->$end_date . ' / ' . $attributes['end_date'] . '<br>';
$html .= 'db_table_participants = ' . $this->$db_table_participants . ' / ' . $attributes['db_table_participants'] . "<br>";
$html .= 'db_table_log = ' . $this->$db_table_log . ' / ' . $attributes['db_table_log'] . '<br>';
return $html;
}
}
The shortcode on the webpage is:
[virtual_crr start_date="2020-06-02" end_date="2020-06-30"]
The var_dump($attributes) returns:
array (size=4)
'start_date' => string '2020-06-02' (length=10)
'end_date' => string '2020-06-30' (length=10)
'db_table_participants' => string 'db_table_participants' (length=21)
'db_table_log' => string 'db_table_log' (length=12)
The output on the webpage is:
start_date = db_table_log / 2020-06-02
end_date = db_table_log / 2020-06-30
db_table_participants = db_table_log / db_table_participants
db_table_log = db_table_log / db_table_log
So clearly I'm not understanding something fundamental as the '$this->$xxxx values differ from the $attributes array values, can you advise please?
Many thanks in advance
Alan
I think you need to remove the $ from your param when you access them with $this.
So it would look more like:
<?php
public function virtual_crr($atts)
{
//******************************
//** Get shortcode parameters **
//******************************
global $post;
$shortcode_defaults = [
'start_date' => '2020-01-01',
'end_date' => '2050-00-01',
'db_table_participants' => 'db_table_participants',
'db_table_log' => 'db_table_log',
];
$attributes = array_merge($shortcode_defaults,$atts);
$this->start_date = $attributes['start_date'];
$this->end_date = $attributes['end_date'];
$this->db_table_participants = $attributes['db_table_participants'];
$this->db_table_log = $attributes['db_table_log'];
var_dump($attributes);
$html = 'start_date = ' . $this->start_date . ' / ' . $attributes['start_date'] . '<br>';
$html .= 'end_date = ' . $this->end_date . ' / ' . $attributes['end_date'] . '<br>';
$html .= 'db_table_participants = ' . $this->db_table_participants . ' / ' . $attributes['db_table_participants'] . "<br>";
$html .= 'db_table_log = ' . $this->db_table_log . ' / ' . $attributes['db_table_log'] . '<br>';
return $html;
}
?>

Order results in left join with db_query()

I'm working on this query for a Drupal 7 project:
$group = taxonomy_get_tree(2);
$group_list = "";
$total_countries_list = "";
foreach ($group as $member) {
$countries_list = "";
$id = str_replace(' ', '-', $member->name);
$id = str_replace('/', '-', $id);
$group_list .= '<li><a id="link-' . str_replace(' ', '-', $id) . '" href="#">. ' . $member->name . '</a></li>';
$results = db_query('SELECT n.title AS name, fdfea.field_email_address_value AS email
FROM node AS n
LEFT JOIN field_data_field_email_address AS fdfea ON fdfea.entity_id = n.nid
LEFT JOIN field_data_field_group_of_countries AS fdfgoc ON fdfgoc.field_group_of_countries_tid = :country_taxonomy
WHERE n.nid = fdfgoc.entity_id', array(':country_taxonomy' => $member->tid));
$i = 0;
foreach ($results as $country) {
if ($i % 13 == 0){
$countries_list .= '</ul><ul>';
}
$countries_list .= '<li>' . $country->name . '</li>';
$i++;
}
$countries_list = '<ul>' . $countries_list . '</ul>';
$total_countries_list .= '<div class="countries-list" id="' . $id . '">' . $countries_list . '</div>';
}
$total_countries_list = '<div class="countries-wrapper">' . $total_countries_list . '</div>';
$group_list = '<ul class="big-list">' . $group_list . '</ul>';
return $group_list . $total_countries_list;
I'm using the results of this query to form lists of countries. The query works fine but I also need to sort countries in each group alphabetically. ORDER BY is apparently not the way to do so so how can I do the sorting?
I'm not sure why you say you can't use ORDER BY.
Assuming the country name is in node.title and there is a group id of some sort in the field_data_field_group_of_countries table (I'm just going to guess something like gid), then surely you could use an ORDER BY clause something like this:
ORDER BY fdfgoc.gid, n.title
So it would order first by the country group, and then by country name in each group.

Break Up WordPress Archive List into Multiple Columns

I want to break up this archive list into multiple columns (three columns)...I don't want to use CSS3 for this. I can easily do this with wp_list_categories, but that method doesn't work with wp_get_archives.
Ideally, I'd like to implement a counter to add in tags at an approximate 1/3 count...then I can use CSS for the rest.
CUSTOM ARCHIVE FUNCTION:
function wp_custom_archive($args = '') {
global $wpdb, $wp_locale;
$defaults = array(
'limit' => '',
'format' => 'html', 'before' => '',
'after' => '', 'show_post_count' => false,
'echo' => 1
);
$r = wp_parse_args( $args, $defaults );
extract( $r, EXTR_SKIP );
if ( '' != $limit ) {
$limit = absint($limit);
$limit = ' LIMIT '.$limit;
}
// over-ride general date format ? 0 = no: use the date format set in Options, 1 = yes: over-ride
$archive_date_format_over_ride = 0;
// options for daily archive (only if you over-ride the general date format)
$archive_day_date_format = 'Y/m/d';
// options for weekly archive (only if you over-ride the general date format)
$archive_week_start_date_format = 'Y/m/d';
$archive_week_end_date_format = 'Y/m/d';
if ( !$archive_date_format_over_ride ) {
$archive_day_date_format = get_option('date_format');
$archive_week_start_date_format = get_option('date_format');
$archive_week_end_date_format = get_option('date_format');
}
//filters
$where = apply_filters('customarchives_where', "WHERE post_type = 'post' AND post_status = 'publish'", $r );
$join = apply_filters('customarchives_join', "", $r);
$output = '<ul>';
$query = "SELECT YEAR(post_date) AS `year`, MONTH(post_date) AS `month`, count(ID) as posts FROM $wpdb->posts $join $where GROUP BY YEAR(post_date), MONTH(post_date) ORDER BY post_date DESC $limit";
$key = md5($query);
$cache = wp_cache_get( 'wp_custom_archive' , 'general');
if ( !isset( $cache[ $key ] ) ) {
$arcresults = $wpdb->get_results($query);
$cache[ $key ] = $arcresults;
wp_cache_set( 'wp_custom_archive', $cache, 'general' );
} else {
$arcresults = $cache[ $key ];
}
if ( $arcresults ) {
$afterafter = $after;
foreach ( (array) $arcresults as $arcresult ) {
$url = get_month_link( $arcresult->year, $arcresult->month );
/* translators: 1: month name, 2: 4-digit year */
$text = sprintf(__('%s'), $wp_locale->get_month($arcresult->month));
$year_text = sprintf('<li>%d</li>', $arcresult->year);
if ( $show_post_count )
$after = ' ('.$arcresult->posts.')' . $afterafter;
$output .= ( $arcresult->year != $temp_year ) ? $year_text : '';
$output .= get_archives_link($url, $text, $format, $before, $after);
$temp_year = $arcresult->year;
}
}
$output .= '</ul>';
if ( $echo )
echo $output;
else
return $output;
}
CURRENT HTML OUTPUT (shortened):
<ul>
<li>2012</li>
<li><a href='http://alittlesewing.com.s118336.gridserver.com/2012/09/' title='September'>September</a></li>
<li><a href='http://alittlesewing.com.s118336.gridserver.com/2012/08/' title='August'>August</a></li>
<li><a href='http://alittlesewing.com.s118336.gridserver.com/2012/07/' title='July'>July</a></li>
<li><a href='http://alittlesewing.com.s118336.gridserver.com/2012/06/' title='June'>June</a></li>
<li><a href='http://alittlesewing.com.s118336.gridserver.com/2012/05/' title='May'>May</a></li>
<li><a href='http://alittlesewing.com.s118336.gridserver.com/2012/04/' title='April'>April</a></li>
<li><a href='http://alittlesewing.com.s118336.gridserver.com/2012/03/' title='March'>March</a></li>
<li><a href='http://alittlesewing.com.s118336.gridserver.com/2012/02/' title='February'>February</a></li>
<li><a href='http://alittlesewing.com.s118336.gridserver.com/2012/01/' title='January'>January</a></li>
</ul>
If I understand correctly, you would have to do something like this
$output = '<ul class="col1">';
if ( $arcresults ) {
$numarticles = count($arcresults);
$column1 = round($numarticles/3);
$column2 = round($column1*2);
$counter = 0;
$afterafter = $after;
foreach ( (array) $arcresults as $arcresult ) {
if($counter == $column1) {
$output .= "</ul><ul class='col2'>";
} elseif($counter == $column2) {
$output .= "</ul><ul class='col3'>";
}
$url = get_month_link( $arcresult->year, $arcresult->month );
/* translators: 1: month name, 2: 4-digit year */
$text = sprintf(__('%s'), $wp_locale->get_month($arcresult->month));
$year_text = sprintf('<li>%d</li>', $arcresult->year);
if ( $show_post_count )
$after = ' ('.$arcresult->posts.')' . $afterafter;
$output .= ( $arcresult->year != $temp_year ) ? $year_text : '';
$output .= get_archives_link($url, $text, $format, $before, $after);
$temp_year = $arcresult->year;
$counter ++;
}
$output .= '</ul>';
}
This way, when the counter gets to a third of the way, it will add another list, so at the end you will have 3 lists.
Hope this helps.

Resources