Showing posts with label date. Show all posts
Showing posts with label date. Show all posts

Select age from birth date in mysql

 To calculate age from a birth date in MySQL, you can use the DATEDIFF function to calculate the difference between the birth date and the current date, and then divide the result by the number of days in a year (365.25) to get the age in years. Here's an example:


SELECT ROUND(DATEDIFF(NOW(), birth_date) / 365.25) AS age
FROM mytable;


In this example, birth_date is the name of the column that contains the birth date, and mytable is the name of the table that contains the birth date column. The NOW() function returns the current date and time, and DATEDIFF calculates the difference between the birth date and the current date in days. The result is then divided by 365.25 (the average number of days in a year) and rounded to the nearest whole number using the ROUND function.

You can modify this query to suit your specific needs, such as by using a different table or column name, or by specifying a specific birth date instead of using NOW(). Keep in mind that this method may not be completely accurate due to leap years, but it should give you a good approximation of the person's age.

Convert php date format to GMT and back to original format

Convert date format to GMT and we can convert back to original format, Here's code to switch to GMT,in this tutorial can convert  date format to GMT date format.

$date = new DateTime();
$date->setTimezone(new DateTimeZone('GMT'));

and back to the default timezone...

$date = new DateTime('2015-01-01', new DateTimeZone('GMT'));
$date->setTimezone(new DateTimeZone(date_default_timezone_get()));

Using the DateTime object lets your create a datetime, just like the procedural functions, except that you keep a reference to an instance.

e.g.

// Get a reference to Christmas of 2015, at lunch time.
$date = new DateTime('2015-12-25 14:00:00');

// Print the date for people to see, in whatever format we specify.
echo $date->format('D jS M y');

// Change the timezone to GMT.
$date->setTimezone(new DateTimeZone('GMT'));

// Now print the date/time it would in the GMT timezone
// as opposed to the default timezone it was created with.
echo $date->format('Y-m-d H:i:s');

// Just to show of some more, get the previous Sunday
$date->modify('previous Sunday');
There's a whole lot of functions you can use, that are much more readable that the procedural functions.

Explicit example of converting from a timezone to GMT

$melbourne = new DateTimeZone('Australia/Melbourne');

$gmt = new DateTimeZone('GMT');

$date = new DateTime('2015-12-25 00:00:00', $melbourne);

$date->setTimezone($gmt);

echo $date->format('Y-m-d H:i:s');

// Output: 2015-12-24 13:00:00

// At midnight on Christmas eve in Melbourne it will be 1pm on Christmas Eve GMT.

echo '<br/>';

// Convert it back to Australia/Melbourne
$date->setTimezone($melbourne);
echo $date->format('Y-m-d H:i:s');

Using your Asia/Kolkata to America/New_York

date_default_timezone_set('Asia/Kolkata');
$date = new DateTime('2015-03-28 13:00:00');
$date->setTimezone(new DateTimeZone('America/New_York'));
echo $date->format("Y-m-d H:i:s");
//Outputs: 2015-03-28 03:30:00

Convert date from dd/mm/yyyy to mm/dd/yyyy or yyyy/mm/dd in php

How to convert date format from dd/mm/yyyy to mm/dd/yyyy its easy to convert date from one format to another in php or can convert mm/dd/yyyy to dd/mm/yyyy also
$date=new DateTime(); //this returns the current date time
$result = $date->format('Y-m-d');
$dat = date('d-M, Y', strtotime(str_replace('/', '-',$result)));
 echo $dat;

  $res = $date->format('Y-m-d');
  $d = date('M-d-Y', strtotime(str_replace('/', '-',$res)));
  echo $d;

$originalDate = "2010-03-21";
$newDate = date("d-m-Y", strtotime($originalDate));

Example #4 date() Formatting

<?php
// Assuming today is March 10th, 2001, 5:16:18 pm, and that we are in the
// Mountain Standard Time (MST) Time Zone

$today = date("F j, Y, g:i a");                 // March 10, 2001, 5:16 pm
$today = date("m.d.y");                         // 03.10.01
$today = date("j, n, Y");                       // 10, 3, 2001
$today = date("Ymd");                           // 20010310
$today = date('h-i-s, j-m-y, it is w Day');     // 05-16-18, 10-03-01, 1631 1618 6 Sat pm 01
$today = date('\i\t \i\s \t\h\e jS \d\a\y.');   // it is the 10th day.
$today = date("D M j G:i:s T Y");               // Sat Mar 10 17:16:18 MST 2001
$today = date('H:m:s \m \i\s\ \m\o\n\t\h');     // 17:03:18 m is month
$today = date("H:i:s");                         // 17:16:18
$today = date("Y-m-d H:i:s");                   // 2001-03-10 17:16:18 (the MySQL DATETIME format)
?>


how to call ssh from vs code

 To call SSH from VS Code, you can use the built-in Remote Development extension. This extension allows you to open a remote folder or works...